WordPress wp_nav_menu() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints or returns a 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.

WordPress
<?php
wp_nav_menu( array( 'theme_location' => 'primary' ) );
CallWhat 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
WordPress
wp_nav_menu( array $args = [] ): void|string|false

Parameters

ParameterDescription
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 ValueDescription
voidWhen echo is true, the menu is printed and nothing is returned.
stringWhen echo is false, the menu HTML.
falseIf there are no items or no menu was found and there is no fallback.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

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.

WordPress
<?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' ) );

How It Works

  1. register_nav_menus() creates the primary location in the admin.
  2. In Appearance > Menus, you assign a menu (here, Main Menu) to it.
  3. 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.

WordPress
<?php
            wp_nav_menu( array(
              'theme_location'  => 'primary',
              'container'       => 'nav',
              'container_class' => 'site-nav',
              'menu_class'      => 'nav-list',
              'menu_id'         => 'primary-menu',
              'depth'           => 1,
            ) );

How It Works

  1. container set to nav wraps the list in a <nav> tag. Use false for no wrapper.
  2. container_class, menu_class, and menu_id give you hooks for your CSS.
  3. 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.

WordPress
<?php
            $menu = wp_nav_menu( array(
              'theme_location' => 'primary',
              'fallback_cb'    => false,
              'echo'           => false,
            ) );
            
            if ( $menu ) {
              echo '<div class="header-menu">' . $menu . '</div>';
            }

How It Works

  1. fallback_cb set to false stops WordPress from showing a list of pages when no menu is assigned.
  2. echo set to false returns the HTML, so we can store it.
  3. If no menu is assigned, $menu is false and nothing is printed.

Frequently Asked Questions

It prints a navigation menu that you built in Appearance > Menus, using the options you pass in.
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.
The most common reason is that no menu is assigned to the theme location. Open Appearance > Menus, pick the location, and save.
Set container to false. You can also set it to nav to use a nav tag.
It runs when no menu is found. The default shows a list of pages. Set it to false to show nothing.
Set echo to false. The function then returns the menu as a string.

Browse all nav menu functions

Open the functions index for simple guides with examples and FAQs.

Nav Menu Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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