Title: walk_nav_menu_tree
Published: April 25, 2014
Last modified: April 28, 2025

---

# walk_nav_menu_tree( array $items, int $depth, stdClass $args ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#wp--skip-link--target)

Retrieves the HTML list content for nav menu items.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#parameters)󠁿

 `$items`arrayrequired

The menu items, sorted by each menu item’s menu order.

`$depth`intrequired

Depth of the item in reference to parents.

`$args`stdClassrequired

An object containing [wp_nav_menu()](https://developer.wordpress.org/reference/functions/wp_nav_menu/)
arguments.

More Arguments from wp_nav_menu( … $args )

Array of nav menu arguments.

 * `menu` int|string|[WP_Term](https://developer.wordpress.org/reference/classes/wp_term/)
 * Desired menu. Accepts a menu ID, slug, name, or object.
 * `menu_class` string
 * CSS class to use for the ul element which forms the menu.
    Default `'menu'`.
 * `menu_id` string
 * The ID that is applied to the ul element which forms the menu.
    Default is the
   menu slug, incremented.
 * `container` string
 * Whether to wrap the ul, and what to wrap it with.
    Default `'div'`.
 * `container_class` string
 * Class that is applied to the container.
    Default ‘menu-{menu slug}-container’.
 * `container_id` string
 * The ID that is applied to the container.
 * `container_aria_label` string
 * The aria-label attribute that is applied to the container when it’s a nav element.
 * `fallback_cb` callable|false
 * If the menu doesn’t exist, a callback function will fire.
    Default is `'wp_page_menu'`.
   Set to false for no fallback.
 * `before` string
 * Text before the link markup.
 * `after` string
 * Text after the link markup.
 * `link_before` string
 * Text before the link text.
 * `link_after` string
 * Text after the link text.
 * `echo` bool
 * Whether to echo the menu or return it. Default true.
 * `depth` int
 * How many levels of the hierarchy are to be included.
    0 means all. Default 0.
   Default 0.
 * `walker` object
 * Instance of a custom walker class.
 * `theme_location` string
 * Theme location to be used. Must be registered with [register_nav_menu()](https://developer.wordpress.org/reference/functions/register_nav_menu/)
   in order to be selectable by the user.
 * `items_wrap` string
 * How the list items should be wrapped. Uses printf() format with numbered placeholders.
   Default is a ul with an id and class.
 * `item_spacing` string
 * Whether to preserve whitespace within the menu’s HTML.
    Accepts `'preserve'` 
   or `'discard'`. Default `'preserve'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#return)󠁿

 string The HTML list content for the menu items.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#source)󠁿

    ```php
    function walk_nav_menu_tree( $items, $depth, $args ) {
    	$walker = ( empty( $args->walker ) ) ? new Walker_Nav_Menu() : $args->walker;

    	return $walker->walk( $items, $depth, $args );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/nav-menu-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/nav-menu-template.php#L618)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/nav-menu-template.php#L618-L622)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#related)󠁿

| Uses | Description | 
| [Walker_Nav_Menu::__construct()](https://developer.wordpress.org/reference/classes/walker_nav_menu/__construct/)`wp-includes/class-walker-nav-menu.php` |

Constructor.

  | 
| [Walker::walk()](https://developer.wordpress.org/reference/classes/walker/walk/)`wp-includes/class-wp-walker.php` |

Displays array of elements hierarchically.

  |

| Used by | Description | 
| [wp_ajax_add_menu_item()](https://developer.wordpress.org/reference/functions/wp_ajax_add_menu_item/)`wp-admin/includes/ajax-actions.php` |

Handles adding a menu item via AJAX.

  | 
| [_wp_ajax_menu_quick_search()](https://developer.wordpress.org/reference/functions/_wp_ajax_menu_quick_search/)`wp-admin/includes/nav-menu.php` |

Prints the appropriate response to a menu quick search.

  | 
| [wp_nav_menu_item_post_type_meta_box()](https://developer.wordpress.org/reference/functions/wp_nav_menu_item_post_type_meta_box/)`wp-admin/includes/nav-menu.php` |

Displays a meta box for a post type menu item.

  | 
| [wp_nav_menu_item_taxonomy_meta_box()](https://developer.wordpress.org/reference/functions/wp_nav_menu_item_taxonomy_meta_box/)`wp-admin/includes/nav-menu.php` |

Displays a meta box for a taxonomy menu item.

  | 
| [wp_get_nav_menu_to_edit()](https://developer.wordpress.org/reference/functions/wp_get_nav_menu_to_edit/)`wp-admin/includes/nav-menu.php` |

Returns the menu formatted to edit.

  | 
| [wp_nav_menu()](https://developer.wordpress.org/reference/functions/wp_nav_menu/)`wp-includes/nav-menu-template.php` |

Displays a navigation menu.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/?output_format=md#comment-content-3834)
 2.   [BlackSwanDev](https://profiles.wordpress.org/blackswanlab/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/walk_nav_menu_tree/#comment-3834)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwalk_nav_menu_tree%2F%23comment-3834)
    Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwalk_nav_menu_tree%2F%23comment-3834)
 4. Add a metabox to WordPress nav-menu and add two predefined links (login, logout).
 5.     ```php
        add_action( 'admin_head-nav-menus.php', function() {
            add_meta_box( 'plugin-slug-menu-metabox', "Wordpress Login/Logout", 'wpdocs_plugin_slug_render_menu_metabox', 'nav-menus', 'side', 'default', array( /*custom params*/ ) );
        } );
    
        function wpdocs_plugin_slug_render_menu_metabox( $object, $args )
        {
          global $nav_menu_selected_id;
          // Create an array of objects that imitate Post objects
          $my_items = array(
            (object) array(
                'ID' => 1,
                'object_id' => 1,
                'type_label' => 'Login',
                'title' => 'Login',
                'url' => wp_login_url(),
                'type' => 'custom',
                'object' => 'plugin-slug-slug',
                'db_id' => 0,
                'menu_item_parent' => 0,
                'post_parent' => 0,
                'target' => '',
                'attr_title' => '',
                'description' => '',
                'classes' => array(),
                'xfn' => '',
            ),
            (object) array(
                'ID' => 1,
                'object_id' => 1,
                'type_label' => 'Logout',
                'title' => 'Logout',
                'url' => wp_logout_url(),
                'type' => 'custom',
                'object' => 'plugin-slug-slug',
                'db_id' => 0,
                'menu_item_parent' => 0,
                'post_parent' => 0,
                'target' => '',
                'attr_title' => '',
                'description' => '',
                'classes' => array(),
                'xfn' => '',
            ),
          );
    
          $db_fields = false;
          // If your links will be hierarchical, adjust the $db_fields array below
          if ( false ) { 
            $db_fields = array( 'parent' => 'parent', 'id' => 'post_parent' ); 
          }
    
          $walker = new Walker_Nav_Menu_Checklist( $db_fields );
          $removed_args = array( 'action', 'customlink-tab', 'edit-menu-item', 'menu-item', 'page-tab', '_wpnonce', );
          ?>
          <div id="plugin-slug-div">
            <div id="tabs-panel-plugin-slug-all" class="tabs-panel tabs-panel-active">
            <ul id="plugin-slug-checklist-pop" class="categorychecklist form-no-clear" >
              <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $my_items ), 0, (object) array( 'walker' => $walker ) ); ?>
            </ul>
            <p class="button-controls">
              <span class="add-to-menu">
                <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-plugin-slug-menu-item" id="submit-plugin-slug-div" />
                <span class="spinner"></span>
              </span>
            </p>
          </div>
          <?php
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwalk_nav_menu_tree%2F%3Freplytocom%3D3834%23feedback-editor-3834)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwalk_nav_menu_tree%2F)
before being able to contribute a note or feedback.