wp_get_nav_menu_name()WP 4.9.0

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

0

#1 Register menu area and display the menu name

  • Register a menu area with the identifier primary and the name Main 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
0

#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.

wp_get_nav_menu_name() code WP 6.5.2

function wp_get_nav_menu_name( $location ) {
	$menu_name = '';

	$locations = get_nav_menu_locations();

	if ( isset( $locations[ $location ] ) ) {
		$menu = wp_get_nav_menu_object( $locations[ $location ] );

		if ( $menu && $menu->name ) {
			$menu_name = $menu->name;
		}
	}

	/**
	 * Filters the navigation menu name being returned.
	 *
	 * @since 4.9.0
	 *
	 * @param string $menu_name Menu name.
	 * @param string $location  Menu location identifier.
	 */
	return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location );
}