Get Menu Values ​​from Wordpress

That's it, I use the following code to get all the specific Wordpress created menus:

$menus = wp_get_nav_menus(); 

I know the identifier of the menu I want to use. Based on the menu identifier, I would like to get the pages that are in this menu, and the corresponding navigation inscription based on the selected menu identifier. How can i do this?

I really discovered this:

 $menu_items = wp_get_nav_menu_items($options['menu_choice']); 

In this example, $ options ['menu_choice'] is the selected menu identifier, but what I really need is to give the value of the permalink. Can I get this from this?

Thanks for any help in advance!

+6
source share
5 answers

To access the title and URL of each item in the menu using the wp_get_nav_menu_items() function:

 $menu_items = wp_get_nav_menu_items( $options['menu_choice'] ); foreach ( (array) $menu_items as $key => $menu_item ) { $title = $menu_item->title; $url = $menu_item->url; } 
+2
source

This is exactly what you want.

 $menu_name = 'menu-id'; //eg primary-menu; $options['menu_choice'] in your case if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) { $menu = wp_get_nav_menu_object($locations[$menu_name]); $menu_items = wp_get_nav_menu_items($menu->term_id); } 

Now $ menu_items is an object containing all the data for all menu items. This way you can get the necessary data using the foreach .

 foreach ($menu_items as $menu_item) { $id = $menu_item->ID; $title = $menu_item->title; $url = $menu_item->url; if ($parent_id = $menu_item->menu_item_parent) { //the element has a parent with id $parent_id, so you can build a hierarchically menu tree } else { //the element doesn't have a parent } } 

You can find more interesting information on this subject, for example, orderby parameters, on the official website: http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

+7
source

Do you want to display a specific menu? why not use the simpler function wp_nav_menu and pass the argument to your desired menu id? just replace the menu_id file with $ menu_ID in the following example:

 <?php $menu_args = array('menu' => $menu_ID ); wp_nav_menu( $menu_args ); ?> 
+2
source

To get the message id, you have to pull it using this function:

$ id = get_post_meta ($ menu_item-> ID, '_menu_item_object_id', true);

Otherwise, the identifier will be a nav_menu type icon that Wordpress uses for the menu. The same goese for $ url, you can name it with get_permalink ($ id);

+1
source
 <pre> $menu_ID = '195'; // 195 is a menu id this id you can see http://example.com/wp-admin/nav-menus.php?action=edit&menu=195 $menu_args = array('menu' => $menu_ID ); wp_nav_menu( $menu_args ); </pre> 
0
source

Source: https://habr.com/ru/post/910743/


All Articles