get_nav_menu_locations(): int[]

Retrieves all registered navigation menu locations and the menus assigned to them.


Return

int[] Associative array of registered navigation menu IDs keyed by their location name. If none are registered, an empty array.


Top ↑

Source

File: wp-includes/nav-menu.php. View all references

function get_nav_menu_locations() {
	$locations = get_theme_mod( 'nav_menu_locations' );
	return ( is_array( $locations ) ) ? $locations : array();
}


Top ↑

Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by madastro

    Allows the editor role to modify menus

    function allow_menu_editor() {
    	$arr_menu = array_filter(get_nav_menu_locations());
    	if ( ! empty($arr_menu) ) {
    		$obj_role = get_role('editor');
    		$obj_role->add_cap('edit_theme_options');
    	}
    }
    add_action( 'admin_menu', 'allow_menu_editor', 999);
  2. Skip to note 2 content
    Contributed by khangaitan
    $menu_name = 'menu1';
    $menu_item = wp_get_nav_menu_object( get_nav_menu_locations( $menu_name )[ $menu_name ] )->name;
    
    echo $menu_item ? $menu_item : __( 'Insert menus', 'textdomain' );
    wp_nav_menu( array(
    	'theme_location' => $menu_name,
    	'container'      => ''
    ) );
    
    $menu_name = '';
    
    $menu_name = 'menu2';
    $menu_item = wp_get_nav_menu_object( get_nav_menu_locations( $menu_name )[ $menu_name ] )->name;
    
    echo $menu_item ? $menu_item : __( 'Insert menus', 'textdomain' );
    wp_nav_menu( array(
    	'theme_location' => $menu_name,
    	'container'      => ''
    ) );
    
    $menu_name = '';
    
    $menu_name = 'menu3';
    $menu_item = wp_get_nav_menu_object( get_nav_menu_locations( $menu_name )[ $menu_name ] )->name;
    
    echo $menu_item ? $menu_item : __( 'Insert menus', 'textdomain' );
    wp_nav_menu( array(
    	'theme_location' => $menu_name,
    	'container'      => ''
    ) );
  3. Skip to note 3 content
    Contributed by Deepesh Dhakal

    Just to Update on the ‘return’ statement, it returns ‘integer’ for individual ‘menu-location’ if no menu is assigned. To be precise, it returns an Array like so:

    print_r(get_nav_menu_locations());

    Returns an array like so

    Array
    (
    [main-menu] => 757
    [mobile-menu] => 1506
    [footer-menu] => 0
    )

    main-menu and mobile-menu are assigned so they return Menu ID whereas footer-menu is unassigned and thus returns ‘0’.

You must log in before being able to contribute a note or feedback.