All, I'm using the following code to get all of the defined wordpress menus that are created:
$menus = wp_get_nav_menus();
I know the ID of the menu I want to use. Based on the menu ID I'd like to get the Pages that are in that menu and the corresponding Navigation Label based on a selected menu ID. How can I go about doing that?
I actually discovered this:
$menu_items = wp_get_nav_menu_items($options['menu_choice']);
In that example the $options['menu_choice'] is the selected Menu ID but what I really would like is to give the permalink value. Can I get that from this?
Thanks for any help in advance!
Show activity on this post. Then do everything you want with this array like so: $menu_items = yourprefix_get_menu_items('sidebar-menu'); // replace sidebar-menu by desired location if(isset($menu_items)){ foreach ( (array) $menu_items as $key => $menu_item ) { ... some code... } }
Get it done in simple way! The code snippet is very simple as the following: $menus = get_terms( 'nav_menu' ); $menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) ); print_r( $menus ); // You'll get: // array( 'menu_id' => 'Menu Name' );
Log into WordPress and go to Appearance > Menus and select the menu you want to get the ID of.
Usage. wp_nav_menu( $args ); Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.
Here are two functions that can be used to add the menu to the REST API and retrieve individual menu items: Create a shortcode that takes a menu ID and prints a simple list of links. Add this to your functions.php file. . $menu_html .
You can get the name like this, using the menu location, so if the menu is updated or you assign other menu you dont have to update anything here: $locations = get_nav_menu_locations(); //get all menu locations $menu = wp_get_nav_menu_object($locations['name_of_the_menu_location']);//get the menu object echo $menu->name; // name of the menu
Show activity on this post. If your theme uses has_nav_menu ('<menu-name>') in the header or footer.php (for example), you can go to Appearance > Menus and select which menu or sub-menu to configure. Highly active question.
You can access the menu metadata using the wp_get_nav_menu_object function Object ( term_id => 4 name => My Menu Name slug => my-menu-name term_group => 0 term_taxonomy_id => 4 taxonomy => nav_menu description => parent => 0 count => 6 ) You are already supplying the name, why would you want to get it again?
That's exactly what you want.
$menu_name = 'menu-id'; //e.g. 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 that contains all data for all menu items. So you can retrieve necessary data using foreach
loop.
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 for this question, such as orderby options, on official website: http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items
To access the title and url of each item in a 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With