Title: Walker::walk
Published: April 25, 2014
Last modified: April 28, 2025

---

# Walker::walk( array $elements, int $max_depth, mixed $args ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#changelog)

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

Displays array of elements hierarchically.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#description)󠁿

Does not assume any existing order of elements.

$max_depth = -1 means flatly display every element.
$max_depth = 0 means display
all levels.$max_depth > 0 specifies the number of display levels.

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

 `$elements`arrayrequired

An array of elements.

`$max_depth`intrequired

The maximum hierarchical depth.

`$args`mixedoptional

Optional additional arguments.

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

 string The hierarchical item output.

## 󠀁[More Information](https://developer.wordpress.org/reference/classes/walker/walk/?output_format=md#more-information)󠁿

This method can be used to initialize the [Walker](https://developer.wordpress.org/reference/classes/walker/)
class. It takes an array of elements ordered so that children occur below their 
parents. The $max_depth parameter is an integer that specifies how deep into the
tree structure the walker should render. By default, the `$max_depth` argument uses`
0`, which will render every item in every branch, with no depth limit. You can also
specify **-1** to render all objects as a “flattened” single-dimensional list. Any
other number will limit the depth that [Walker](https://developer.wordpress.org/reference/classes/walker/)
will render in any branch. Any _additional_ arguments passed to this method will
be passed unchanged to the other methods.

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

    ```php
    public function walk( $elements, $max_depth, ...$args ) {
    	$output = '';

    	$max_depth = (int) $max_depth;

    	// Invalid parameter or nothing to walk.
    	if ( $max_depth < -1 || empty( $elements ) ) {
    		return $output;
    	}

    	$parent_field = $this->db_fields['parent'];

    	// Flat display.
    	if ( -1 === $max_depth ) {
    		$empty_array = array();
    		foreach ( $elements as $e ) {
    			$this->display_element( $e, $empty_array, 1, 0, $args, $output );
    		}
    		return $output;
    	}

    	/*
    	 * Need to display in hierarchical order.
    	 * Separate elements into two buckets: top level and children elements.
    	 * Children_elements is two dimensional array. Example:
    	 * Children_elements[10][] contains all sub-elements whose parent is 10.
    	 */
    	$top_level_elements = array();
    	$children_elements  = array();
    	foreach ( $elements as $e ) {
    		if ( empty( $e->$parent_field ) ) {
    			$top_level_elements[] = $e;
    		} else {
    			$children_elements[ $e->$parent_field ][] = $e;
    		}
    	}

    	/*
    	 * When none of the elements is top level.
    	 * Assume the first one must be root of the sub elements.
    	 */
    	if ( empty( $top_level_elements ) ) {

    		$first = array_slice( $elements, 0, 1 );
    		$root  = $first[0];

    		$top_level_elements = array();
    		$children_elements  = array();
    		foreach ( $elements as $e ) {
    			if ( $root->$parent_field === $e->$parent_field ) {
    				$top_level_elements[] = $e;
    			} else {
    				$children_elements[ $e->$parent_field ][] = $e;
    			}
    		}
    	}

    	foreach ( $top_level_elements as $e ) {
    		$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
    	}

    	/*
    	 * If we are displaying all levels, and remaining children_elements is not empty,
    	 * then we got orphans, which should be displayed regardless.
    	 */
    	if ( ( 0 === $max_depth ) && count( $children_elements ) > 0 ) {
    		$empty_array = array();
    		foreach ( $children_elements as $orphans ) {
    			foreach ( $orphans as $op ) {
    				$this->display_element( $op, $empty_array, 1, 0, $args, $output );
    			}
    		}
    	}

    	return $output;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-walker.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-walker.php#L194)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-walker.php#L194-L269)

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

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

Traverses elements to create list from elements.

  |

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

Outputs an unordered list of checkbox input elements labelled with term names.

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

Retrieves HTML list content for category list.

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

Retrieves HTML dropdown (select) content for category list.

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

Retrieves the HTML list content for nav menu items.

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

Retrieves HTML list content for page list.

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

Retrieves HTML dropdown (select) content for page list.

  |

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

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

| Version | Description | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Formalized the existing `...$args` parameter by adding it to the function signature. | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## User Contributed Notes

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