Home WordPress Nav Menus Functions wp_nav_menu() Definition In WordPress, wp_nav_menu() shows a navigation menu on your site. You build the menu in Appearance > Menus in the admin, and this function prints it inside your theme.
Themes usually use theme_location. You register a location such as primary in functions.php, assign a menu to it in the admin, and then show it with wp_nav_menu().
Syntax Pass one array of options. Most themes only need theme_location.
<?php
wp_nav_menu( array( 'theme_location' => 'primary' ) ); Call What it does wp_nav_menu()Use the defaults. WordPress picks a menu, or uses the fallback. wp_nav_menu( array( 'theme_location' => 'primary' ) )Show the menu assigned to the primary location. wp_nav_menu( array( 'menu' => 'Main Menu' ) )Show a menu by its name, slug, or ID. wp_nav_menu( array( ..., 'echo' => false ) )Return the HTML instead of printing it.
By default it prints the menu and returns nothing. Set echo to false to get a string back.
Full declaration wp_nav_menu( array $args = [] ): void|string|false Parameters Parameter Description theme_locationstring. The registered location to show, such as primary. menuint|string|WP_Term. A menu ID, slug, name, or object. theme_location is used first if it has a menu. containerstring|false. The tag around the list. Default is div. Use nav, or false for no wrapper. container_classstring. CSS class of the container. Default is menu-{menu slug}-container. container_idstring. ID of the container. Default is empty. menu_classstring. CSS class of the ul list. Default is menu. menu_idstring. ID of the ul list. Default is the menu slug. echobool. true prints the menu. false returns it. Default is true. fallback_cbcallable|false. What to run if no menu is found. Default is wp_page_menu. Use false for nothing. before, afterstring. Text or HTML placed before and after each link. link_before, link_afterstring. Text or HTML placed before and after the link text. items_wrapstring. The HTML of the list. Default is <ul id="%1$s" class="%2$s">%3$s</ul>. depthint. How many levels to show. 0 shows all levels. Default is 0. walkerobject. A custom walker. Default is empty, which uses Walker_Nav_Menu.
Return Value Return Value Description void When echo is true, the menu is printed and nothing is returned. string When echo is false, the menu HTML. false If there are no items or no menu was found and there is no fallback.
Important
A location needs a menu. register_nav_menus() only creates the place. You must also open Appearance > Menus and assign a menu to it. If you do not, WordPress may show a page list through fallback_cb, so set it to false if you want nothing.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Show a menu in the header 2. Use a nav tag and your own classes 3. No fallback, and return the HTML Example 1 — Show a menu in the header Register a location, assign a menu in the admin, and print it. Real WordPress adds more CSS classes than shown here.
<?php
// functions.php
function mytheme_menus() {
register_nav_menus( array(
'primary' => 'Primary Menu',
) );
}
add_action( 'after_setup_theme', 'mytheme_menus' );
// header.php
wp_nav_menu( array( 'theme_location' => 'primary' ) ); <div class="menu-main-menu-container"><ul id="menu-main-menu" class="menu">
<li id="menu-item-10" class="menu-item"><a href="https://example.com/">Home</a></li>
<li id="menu-item-11" class="menu-item"><a href="https://example.com/about/">About</a></li>
<li id="menu-item-12" class="menu-item"><a href="https://example.com/contact/">Contact</a></li>
</ul></div> How It Works register_nav_menus() creates the primary location in the admin.In Appearance > Menus , you assign a menu (here, Main Menu ) to it. wp_nav_menu() prints that menu where you call it.Example 2 — Use a nav tag and your own classes Change the wrapper tag, the CSS classes, and the number of levels.
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'container' => 'nav',
'container_class' => 'site-nav',
'menu_class' => 'nav-list',
'menu_id' => 'primary-menu',
'depth' => 1,
) ); <nav class="site-nav"><ul id="primary-menu" class="nav-list">
<li id="menu-item-10" class="menu-item"><a href="https://example.com/">Home</a></li>
<li id="menu-item-11" class="menu-item"><a href="https://example.com/about/">About</a></li>
</ul></nav> How It Works container set to nav wraps the list in a <nav> tag. Use false for no wrapper.container_class, menu_class, and menu_id give you hooks for your CSS.depth set to 1 shows only top-level items.Example 3 — No fallback, and return the HTML Stop the page list from showing, and keep the HTML in a variable.
<?php
$menu = wp_nav_menu( array(
'theme_location' => 'primary',
'fallback_cb' => false,
'echo' => false,
) );
if ( $menu ) {
echo '<div class="header-menu">' . $menu . '</div>';
} <div class="header-menu"><div class="menu-main-menu-container"><ul id="menu-main-menu" class="menu">
<li id="menu-item-10" class="menu-item"><a href="https://example.com/">Home</a></li>
<li id="menu-item-11" class="menu-item"><a href="https://example.com/about/">About</a></li>
</ul></div></div> How It Works fallback_cb set to false stops WordPress from showing a list of pages when no menu is assigned.echo set to false returns the HTML, so we can store it.If no menu is assigned, $menu is false and nothing is printed. Frequently Asked Questions What does wp_nav_menu() do? It prints a navigation menu that you built in Appearance > Menus, using the options you pass in.
How do I show a menu in my theme? Register a location with register_nav_menus() in functions.php, assign a menu to it in Appearance > Menus, and call wp_nav_menu() with theme_location.
Why does my menu not show? The most common reason is that no menu is assigned to the theme location. Open Appearance > Menus, pick the location, and save.
How do I remove the div around the menu? Set container to false. You can also set it to nav to use a nav tag.
What is fallback_cb for? It runs when no menu is found. The default shows a list of pages. Set it to false to show nothing.
How do I get the menu HTML instead of printing it? Set echo to false. The function then returns the menu as a string.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion