wp_get_nav_menu_name()
Returns the name of a navigation menu.
Hooks from the function
Return
String
. Menu name.
Usage
wp_get_nav_menu_name( $location );
- $location(string) (required)
- Menu location identifier.
Examples
#1 Register menu area and display the menu name
- Register a menu area with the identifier
primary
and the nameMain Menu
. - In the admin panel, create a menu called "Header Menu" and give it a menu area of
Main Menu
. - Display the menu name assigned to the menu area with the identifier
primary
.
// Register the "Main menu" menu area with the identifier "primary" add_action( 'after_setup_theme', 'theme_register_nav_menu' ); function theme_register_nav_menu() { register_nav_menu('primary', 'Main menu' ); } // Get the name of the menu assigned to the "primary" area $name_menu = wp_get_nav_menu_name( $location ); // Menu name output echo $name_menu; //> Header menu
#2 Output the name of the menu before displaying the menus items to the user
$locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object( $locations['registered-menu-location-slug'] ); echo '<div class="footer-menu__title">' . esc_html( $menu->name ) . '</div>';
Changelog
Since 4.9.0 | Introduced. |