WordPress navigation menus FAQ
The register_nav_menus()
function is used to register custom navigation menus for a WordPress theme.
It allows theme developers to create multiple custom menus and display them in different locations on a WordPress site.
This function should be called in the theme’s functions.php
file to register the custom menus.
Once a custom menu has been registered, it can be displayed in a theme using the wp_nav_menu()
function.
The wp_nav_menu()
function is a built-in WordPress function that is used to display navigation menus on your website.
It is commonly used in theme templates to display a site’s main navigation menu in the header or footer.
The function accepts several parameters, such as the theme_location
parameter, which specifies the location of the menu on the site, and the menu_class
parameter, which sets the CSS class for the menu’s container.
Here is an example of how the function might be used in a theme template:wp_nav_menu( array( 'theme_location' => 'header-menu', 'menu_class' => 'nav-menu', ) );
In your WordPress dashboard, go to Appearance > Menus. Then, create a new menu or select an existing one.
Add pages, posts, or custom links to the menu. You can use the drag-and-drop feature to arrange the items in the order you want them to appear.
Finally, select the location for the menu to be displayed (e.g. header, footer) and click the Save Menu button
It’s also possible to customize the appearance of the menu using the theme’s Customizer.
WordPress navigation menus are stored in the database as custom post types (CPT) with the type nav_menu_item
. Each menu item, whether it’s a page, post, category, or custom link, is stored as a separate post within this CPT.
The relationship between the menu items and the actual menus themselves are stored in the wp_term_relationships
table, where the term_taxonomy_id
refers to the menu, and the object_id
refers to the menu item.
When a theme calls the wp_nav_menu()
function, WordPress uses the specified theme location or menu name to query the wp_term_relationships
table and retrieve the associated menu items.
It then loops through the items and generates the appropriate HTML markup to display the menu on the website.
A navigation menu refers to the actual collection of menu items that make up the site’s navigation. It can be created, edited and deleted from the WordPress Dashboard under Appearance > Menus.
A navigation menu can contain a variety of items such as pages, posts, categories, custom links, etc. and it can be displayed in multiple locations on a website.
On the other hand, a menu location refers to the specific area on a website where a navigation menu is displayed. A menu location is defined by the theme developer and it can be registered using the register_nav_menu()
function.
Once registered, a menu location can be assigned to a navigation menu from the WordPress Dashboard under Appearance > Menus > Manage Locations.
For example, a theme may have two menu locations: header-menu
and footer-menu
. The developer can assign a different navigation menu to each location, so the header menu and the footer menu can be different. Then, using the wp_nav_menu()
function in the theme templates, the developer can specify which menu location to display, and the corresponding navigation menu will be displayed in that location on the website.
To better understand how navigation menus are working, refer to the official WordPress documentation and the register_nav_menu()
function reference.