Home WordPress Nav Menus Functions walk_nav_menu_tree() Definition In WordPress, walk_nav_menu_tree() builds the HTML for a list of menu items. It hands the items to a Walker (usually Walker_Nav_Menu) that goes through every item, and through each child item too, and returns the result as a string.
This is an advanced helper. Most beginners never call it, because wp_nav_menu() calls it for you. Use it only when you already have menu item objects and want to make your own menu HTML.
Syntax Get the menu items, then pass them in with a depth and an arguments object.
<?php
echo walk_nav_menu_tree( $items, 0, $args ); Call What it does walk_nav_menu_tree( $items, 0, $args )Walk all levels of the menu (depth 0 means no limit). walk_nav_menu_tree( $items, 1, $args )Show only the top-level items. walk_nav_menu_tree( $items, -1, $args )Show every item in one flat list, with no nesting.
It returns a string of <li> items. It does not add the outer <ul>, so you add that yourself.
Full declaration walk_nav_menu_tree( array $menu_items, int $depth, stdClass $args ): string Parameters Parameter Description $itemsarray. The menu item objects, for example from wp_get_nav_menu_items(). $depthint. How many levels to show. 0 shows all levels, 1 shows only the top level, and -1 shows a flat list. $argsobject. Settings for the walker. It usually has before, after, link_before, link_after, and walker. If walker is empty, Walker_Nav_Menu is used.
Return Value Return Value Description string The HTML for the menu items. It is an empty string if there are no items.
Important
Prefer wp_nav_menu() for normal themes. It gets the items, adds classes like current-menu-item, wraps the list, and then calls this function. If you call walk_nav_menu_tree() yourself, you get plain items without those extras.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Walk a menu with the default walker 2. Show only the top level 3. Use your own walker Example 1 — Walk a menu with the default walker Get the items of a menu named main-menu and build a list.
<?php
$items = wp_get_nav_menu_items( 'main-menu' );
if ( $items ) {
foreach ( $items as $item ) {
$item->current = false; // The default walker reads this.
}
$args = (object) array(
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'walker' => '',
);
echo '<ul>' . walk_nav_menu_tree( $items, 0, $args ) . '</ul>';
} <ul><li id="menu-item-10" class="menu-item-10"><a href="https://example.com/">Home</a></li>
<li id="menu-item-11" class="menu-item-11"><a href="https://example.com/about/">About</a></li>
<li id="menu-item-12" class="menu-item-12"><a href="https://example.com/contact/">Contact</a></li>
</ul> How It Works wp_get_nav_menu_items() returns the items of the menu.The $args object tells the walker there is no extra text around links. An empty walker means Walker_Nav_Menu. walk_nav_menu_tree() returns the <li> items, and we add the <ul> ourselves.Example 2 — Show only the top level Use depth 1 to hide sub menu items.
<?php
$items = wp_get_nav_menu_items( 'main-menu' );
if ( $items ) {
foreach ( $items as $item ) {
$item->current = false;
}
$args = (object) array(
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'walker' => '',
);
echo '<ul class="footer-links">' . walk_nav_menu_tree( $items, 1, $args ) . '</ul>';
} <ul class="footer-links"><li id="menu-item-10" class="menu-item-10"><a href="https://example.com/">Home</a></li>
<li id="menu-item-11" class="menu-item-11"><a href="https://example.com/blog/">Blog</a></li>
</ul> How It Works Here the menu has a child item News under Blog . Depth 1 shows only the top-level items, so News is left out. This is handy for a short footer list. Example 3 — Use your own walker Extend Walker_Nav_Menu and change how each item is printed.
<?php
class Simple_Menu_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
$output .= '<li><a href="' . esc_url( $data_object->url ) . '">'
. esc_html( $data_object->title ) . '</a>';
}
}
$items = wp_get_nav_menu_items( 'main-menu' );
if ( $items ) {
$args = (object) array( 'walker' => new Simple_Menu_Walker() );
echo '<ul class="simple-menu">' . walk_nav_menu_tree( $items, 0, $args ) . '</ul>';
} <ul class="simple-menu"><li><a href="https://example.com/">Home</a></li>
<li><a href="https://example.com/about/">About</a></li>
<li><a href="https://example.com/contact/">Contact</a></li>
</ul> How It Works Simple_Menu_Walker changes only start_el(), which prints one menu item.We pass the walker object in $args, so walk_nav_menu_tree() uses it. The parent class still closes each </li> and handles sub menus. Frequently Asked Questions What does walk_nav_menu_tree() do? It goes through a list of menu items with a Walker and returns the HTML list items as a string.
Do beginners need walk_nav_menu_tree()? No. wp_nav_menu() calls it for you. Use it only when you build custom menu HTML from menu item objects.
What is a Walker in WordPress? A Walker is a class that visits items in a tree, such as menu items and their children. Walker_Nav_Menu is the one used for navigation menus.
What does the depth value mean? Zero shows all levels, one shows only the top level, and minus one shows all items in one flat list.
Does it add the outer ul tag? No. It returns only the list items. You add the surrounding ul, or let wp_nav_menu() do it with items_wrap.
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.
6 people found this page helpful
Helpful Share Copy link Suggestion