Home WordPress Nav Menus Definition
Definition A nav menu is a list of links, like Home, About, and Contact. In WordPress you build it in the admin under Appearance > Menus , so you can add, remove, and reorder links without code.
Your theme decides where a menu shows. It does this with theme locations , for example primary for the header and footer for the footer. The theme prints the menu with wp_nav_menu() .
Three steps. 1) Register a location in functions.php. 2) Assign a menu to it in Appearance > Menus . 3) Print it with wp_nav_menu().
Syntax
Syntax Register a location, then show it.
<?php
// functions.php
register_nav_menus( array( 'primary' => 'Primary Menu' ) );
// header.php
wp_nav_menu( array( 'theme_location' => 'primary' ) ); Reference
Locations and Menus A location is a place in the theme. A menu is the list of links you put there.
Part Where you set it Function Theme location functions.phpregister_nav_menus()Menu and its links Appearance > Menus Admin screen Show the menu Theme template, such as header.php wp_nav_menu()
register_nav_menus() takes an array of slug => label. The label is what the admin sees. Use register_nav_menu() (singular) when you have only one location.
Compare
wp_nav_menu() vs walk_nav_menu_tree() wp_nav_menu() is the everyday function. walk_nav_menu_tree() is a helper inside it.
wp_nav_menu() walk_nav_menu_tree() Job Show a full menu Build the list items from menu items Who uses it Almost every theme Advanced custom output Needs a walker? No (it uses Walker_Nav_Menu) Yes, passed in $args or the default one Guide wp_nav_menu()walk_nav_menu_tree()
Custom walkers. If you need special HTML, such as extra tags for a dropdown, write a class that extends Walker_Nav_Menu and pass it as the walker option of wp_nav_menu(). That is when walk_nav_menu_tree() matters.
Code
WordPress Examples Two short examples with sample output.
1. Show the primary menu 2. Use a nav tag and no fallback Example 1 — Show the primary menu Register a location, then print its menu. The sample output shows fewer CSS classes than WordPress adds.
<?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>
</ul></div> How It Works register_nav_menus() adds the primary location, and after_setup_theme is the right time to do it.In Appearance > Menus , assign a menu to Primary Menu . wp_nav_menu() prints the menu where you call it.Example 2 — Use a nav tag and no fallback Wrap the list in <nav> and show nothing if no menu is assigned.
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'container' => 'nav',
'menu_class' => 'nav-list',
'fallback_cb' => false,
) ); <nav class="menu-main-menu-container"><ul id="menu-main-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 uses a <nav> tag instead of a <div>.menu_class sets the CSS class of the list.fallback_cb set to false prints nothing if no menu is assigned.Continue
Next: WordPress Nav Menu Functions Now that you know the basics, open the functions index for both guides.
Full list: WordPress Nav Menu Functions index (/wordpress/nav-menu/function).
Frequently Asked Questions What is a WordPress nav menu? It is a list of links, such as Home, About, and Contact, that you build in Appearance > Menus and show in your theme.
What is a theme location? It is a named spot in your theme, such as primary or footer. You register it with register_nav_menus() and assign a menu to it in the admin.
Which function shows a menu? wp_nav_menu(). Pass theme_location to show the menu assigned to that location.
Why is my menu empty or showing pages? No menu is assigned to the location. Assign one in Appearance > Menus, or set fallback_cb to false to show nothing.
Do I need walk_nav_menu_tree()? Not for normal themes. wp_nav_menu() calls it for you. It matters when you write a custom walker or build custom menu HTML.
Where can I see every nav menu function? Open the WordPress Nav Menu Functions index. It lists both guides in short tables.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion