Menu Details Output in Javascript

I have to output page, parameters( Page Url, Page Name, and Page's featured image) in javascript loop, one by one

I think we can display it through NavMenuItems or NavMenu functions

Pages displayed will be those a user is allowed to see e by user hierarchy,

Any suggestions on how to proceed...??

Topic plugin-wp-pagenavi navigation menus Wordpress javascript

Category Web


You can just use wp_get_nav_menu_items( $menu, $args ); to get list of menu items created in appearance.

You can print result the way you want. Before printing just make sure the datas are not empty and so. I didn't add any validation. So use according to your need.

For example:

We'll try to print the data at wp_head

add_action('wp_head','menu_to_js');
function menu_to_js(){
  $locations = get_nav_menu_locations();
  $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );//$menu_name should be your menu name
  $menu_items = wp_get_nav_menu_items($menu->term_id);
  $menu_items = json_encode($menu_items);

  //So now we have json encoded $menu_items, put that in javascript 
   ?>
      <script>
       var menu_itemss = "<?php echo $menu_items; ?>";
       //now manipulate in js and use the way you want.
      </script> 
    <?php
   }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.