2

I'm currently setting up a sidebar menu with multiple menus and sections. Each section will a title (the menu name) and a bunch of links underneath (the menu items) - I printed the items, but how do I print the menu name?

Thanks,

Jacob

3

You can access the menu metadata using the wp_get_nav_menu_object function

BY NAME:

$menu = wp_get_nav_menu_object("my mainmenu" );

BY SLUG:

$menu = wp_get_nav_menu_object("my-mainmenu" );

The return object as follows:

 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
 )

To display the name:

echo $menu->name;
  • You are already supplying the name, why would you want to get it again? Each time he changes the menu for another or updates the menu name in the back-end, he will have to update those values. – David Lee Feb 18 '17 at 19:12
  • @DavidLee I don't get it why you discarded my answer, the idea is the same, I just provided the possible parameter accepted by wp_get_nav_menu_object function, that is it. – Den Isahac Feb 18 '17 at 19:21
  • If you'd also like you can access the menu meta data by supplying the ID of the menu as follows $menu = wp_get_nav_menu_object ('id'); – Den Isahac Feb 18 '17 at 19:26
  • think about it, you are saying that to get the menu name you need the menu name, same with the ID – David Lee Feb 18 '17 at 19:41
  • Hey David, I think you're making a great point - but I don't quite understand it. Could you explain a bit more in your answer? – Jacob Henning Feb 21 '17 at 0:59
3

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

the 'name_of_the_menu_location' is the one you use to output a menu using wp_nav_menu

<?php
     wp_nav_menu(array(
          'theme_location' => 'footer'//this value
     ));
?>
0

On WordPress version 4.9.0 and above you can use

wp_get_nav_menu_name($location)

wp_nav_menu_name for more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.