WordPress walk_nav_menu_tree() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a string

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.

WordPress
<?php
echo walk_nav_menu_tree( $items, 0, $args );
CallWhat 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
WordPress
walk_nav_menu_tree( array $menu_items, int $depth, stdClass $args ): string

Parameters

ParameterDescription
$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 ValueDescription
stringThe HTML for the menu items. It is an empty string if there are no items.

WordPress Examples

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

Example 1 — Walk a menu with the default walker

Get the items of a menu named main-menu and build a list.

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

How It Works

  1. wp_get_nav_menu_items() returns the items of the menu.
  2. The $args object tells the walker there is no extra text around links. An empty walker means Walker_Nav_Menu.
  3. 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.

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

How It Works

  1. Here the menu has a child item News under Blog.
  2. Depth 1 shows only the top-level items, so News is left out.
  3. 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.

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

How It Works

  1. Simple_Menu_Walker changes only start_el(), which prints one menu item.
  2. We pass the walker object in $args, so walk_nav_menu_tree() uses it.
  3. The parent class still closes each </li> and handles sub menus.

Frequently Asked Questions

It goes through a list of menu items with a Walker and returns the HTML list items as a string.
No. wp_nav_menu() calls it for you. Use it only when you build custom menu HTML from menu item objects.
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.
Zero shows all levels, one shows only the top level, and minus one shows all items in one flat list.
No. It returns only the list items. You add the surrounding ul, or let wp_nav_menu() do it with items_wrap.

Next: wp_nav_menu()

Display a menu in your theme, with simple options.

wp_nav_menu() tutorial →

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.

6 people found this page helpful