register_nav_menus( array $locations = array() )

Registers navigation menu locations for a theme.


Description Description


Parameters Parameters

$locations

(array) (Optional) Associative array of menu location identifiers (like a slug) and descriptive text.

Default value: array()


Top ↑

Source Source

File: wp-includes/nav-menu.php

function register_nav_menus( $locations = array() ) {
	global $_wp_registered_nav_menus;

	add_theme_support( 'menus' );

	foreach ( $locations as $key => $value ) {
		if ( is_int( $key ) ) {
			_doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' );
			break;
		}
	}

	$_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
}

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 SeherKhan
    if ( ! function_exists( 'mytheme_register_nav_menu' ) ) {
    
    	function mytheme_register_nav_menu(){
    		register_nav_menus( array(
    	    	'primary_menu' => __( 'Primary Menu', 'text_domain' ),
    	    	'footer_menu'  => __( 'Footer Menu', 'text_domain' ),
    		) );
    	}
    	add_action( 'after_setup_theme', 'mytheme_register_nav_menu', 0 );
    }
    
  2. Skip to note 2 content
    Contributed by Khoi Pro

    Creating menus from your Custom Taxonomies.

    For example: I have a custom taxonomy named ‘Country’ with a few countries in list.
    Right now I want to assign each country has a private name and using it for condition displayed on frontend.

    $tax = 'country-category';
    
    $terms = get_terms( $tax, [
    'hide_empty' => false,
    ]);
    
    $args = array(
    'primary' => __( 'Primary Menu', 'khoipro' ),
    'secondary' => __( 'Secondary Menu', 'khoipro' )
    );
    
    // Loop through all terms to add term id as menu id and term name as menu name.
    foreach( $terms as $term) {
    $args = array_merge($args, array(
    'primary_'.$term->slug => 'Country Menu ('.$term->name.')'
    ));
    }
    
    register_nav_menus( $args );
    

    So my output codes should be displayed in Menus > Manage Locations:

    * Primary Menu (id: primary)
    * Secondary Menu (id: secondary)
    * Country Menu (Japan) (id: primary_japan)
    * Country Menu (Singapore) (id: primary_singapore)
    * Country menu (Vietnam) (id: primary_vietnam)

    Have a proof in a real practice with a newly created project.

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