Registers navigation menu locations for a theme.
Parameters
$locations
string[]optional- Associative array of menu location identifiers (like a slug) and descriptive text.
Default:
array()
Source
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 );
}
Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |
Refactoring Menu Slugs:
If you just rename the slug of a registered menu location, it will disappear from the frontend. This means you need to reassign the menu to the new location in the dashboard.
In order to edit the slug of a menu location while automatically keeping the previously assigned menu, customize and use the following snippet after renaming the slug in
register_nav_menu()
:Also, don’t forget to update the slug where it’s used to display the menu (
wp_nav_menu()
,has_nav_menu()
).You can use get_registered_nav_menus() to verify that the
old-slug
is not registered in your theme, before removing the assigned menu.Example
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.
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.