How do I get the name of a menu in WordPress?

I'm currently setting up a sidebar menu with multiple menus and sections. Each section with the title (the menu nam

I'm currently setting up a sidebar menu with multiple menus and sections. Each section with the 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

I'm currently setting up a sidebar menu with multiple menus and sections. Each section with the 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

Share Improve this question edited Jun 18, 2020 at 7:30 NinjaMAN 1031 silver badge3 bronze badges asked Feb 18, 2017 at 18:55 Jacob HenningJacob Henning 2031 gold badge2 silver badges6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

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;

On WordPress version 4.9.0 and above you can use

wp_get_nav_menu_name($location)

wp_nav_menu_name for more

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
     ));
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742338070a4425054.html

相关推荐

  • How do I get the name of a menu in WordPress?

    I'm currently setting up a sidebar menu with multiple menus and sections. Each section with the title (the menu nam

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信