register_nav_menus() WordPress Function
The register_nav_menus() function is used to create custom menus for your website. This function takes two arguments: the first is the name of the menu, and the second is an array of menu items. To use this function, first create a custom menu in your Wordpress admin panel. Then, add the following code to your theme's functions.php file: __( 'Main Menu' ), 'secondary-menu' => __( 'Secondary Menu' ), ) ); ?> This code will register two custom menus: "Main Menu" and "Secondary Menu". You can then use these menus in your theme by calling the wp_nav_menu() function.
register_nav_menus( string[] $locations = array() ) #
Registers navigation menu locations for a theme.
Parameters
- $locations
(string[])(Optional)Associative array of menu location identifiers (like a slug) and descriptive text.
Default value: array()
More Information
See register_nav_menu() for creating a single menu, and Navigation Menus for adding theme support.
This function automatically registers custom menu support for the theme, therefore you do not need to call add_theme_support( 'menus' );
Use wp_nav_menu() to display your custom menu.
On the Menus admin page, there is a Show advanced menu properties to allow “Link Target” “CSS Classes” “Link Relationship (XFN) Description”
Use get_registered_nav_menus to get back a list of the menus that have been registered in a theme.
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 );
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |