get_nav_menu_locations()

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


Return Return

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


Top ↑

Source Source

File: wp-includes/nav-menu.php

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


Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes 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'      => ''
    ) );
    

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