WordPress at a glance

register_nav_menus() WP 3.0.0

Registers multiple custom navigation menus locations for a theme, to which the menus are then attached.

WP Menus are setup in admin-panel in the Appearence > Menus section and printed with wp_nav_menu() function.

When setting up a menu, you can add different links (items) there: categories, tags, pages, posts, external links, etc.

Registration of a custom menu for the admin panel

It is recommended to call this function on after_setup_theme hook.

This function automatically adds menu support for the theme, so there's no need to call add_theme_support('menus') if you are using it.

There are advanced menu settings on the Menus admin page in the Screen Options tab. It allows you to specify various attributes of the HTML links of the menu items.

Is the basis for: register_nav_menu()

No Hooks.

Return

Null. Nothing.

Usage

register_nav_menus( $locations );
$locations(array)
Associative array of menu location identifiers (like a slug) and descriptive text.
Default: array()

Examples

#1 An example of registering multiple menus

It also automatically adds menu support to the theme.

add_action( 'after_setup_theme', function(){
	register_nav_menus( [
		'header_menu' => 'Header menu',
		'footer_menu' => 'Footer menu'
	] );
} );

We will display these menus with wp_nav_menu().

Notes

  • Global. Array. $_wp_registered_nav_menus

Changelog

Since 3.0.0 Introduced.

Code of register nav menus: wp-includes/nav-menu.php WP 5.2.3

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

	add_theme_support( 'menus' );

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

Related Functions

From tag: nav menu (nav_menu)

More from category: Other Theme Functions

No comments
    Hello, !     Log In . Register