Title: Walker::display_element
Published: April 25, 2014
Last modified: May 20, 2026

---

# Walker::display_element( object $element, array $children_elements, int $max_depth, int $depth, array $args, string $output )

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/Walker/display_element/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/Walker/display_element/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/classes/Walker/display_element/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/Walker/display_element/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/Walker/display_element/?output_format=md#changelog)

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

Traverses elements to create list from elements.

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

Display one element if the element doesn’t have any children otherwise, display 
the element and its children. Will only traverse up to the max depth and no ignore
elements under that depth. It is possible to set the max depth to include all depths,
see walk() method.

This method should not be called directly, use the walk() method instead.

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

 `$element`objectrequired

Data object.

`$children_elements`arrayrequired

List of elements to continue traversing (passed by reference).

`$max_depth`intrequired

Max depth to traverse.

`$depth`intrequired

Depth of current element.

`$args`arrayrequired

An array of arguments.

`$output`stringrequired

Used to append additional content (passed by reference).

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

    ```php
    public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
    	if ( ! $element ) {
    		return;
    	}

    	$max_depth = (int) $max_depth;
    	$depth     = (int) $depth;

    	$id_field = $this->db_fields['id'];
    	$id       = $element->$id_field;

    	// Display this element.
    	$this->has_children = ! empty( $children_elements[ $id ] );
    	if ( isset( $args[0] ) && is_array( $args[0] ) ) {
    		$args[0]['has_children'] = $this->has_children; // Back-compat.
    	}

    	$this->start_el( $output, $element, $depth, ...array_values( $args ) );

    	// Descend only when the depth is right and there are children for this element.
    	if ( ( 0 === $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {

    		foreach ( $children_elements[ $id ] as $child ) {

    			if ( ! isset( $newlevel ) ) {
    				$newlevel = true;
    				// Start the child delimiter.
    				$this->start_lvl( $output, $depth, ...array_values( $args ) );
    			}
    			$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
    		}
    		unset( $children_elements[ $id ] );
    	}

    	if ( isset( $newlevel ) && $newlevel ) {
    		// End the child delimiter.
    		$this->end_lvl( $output, $depth, ...array_values( $args ) );
    	}

    	// End this element.
    	$this->end_el( $output, $element, $depth, ...array_values( $args ) );
    }
    ```

[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/7.0/src/wp-includes/class-wp-walker.php#L133)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-walker.php#L133-L174)

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

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

Starts the element output.

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

Starts the list before the elements are added.

  | 
| [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.

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

Ends the list of after the elements are added.

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

Ends the element output, if needed.

  |

| Used by | 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.

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

Displays array of elements hierarchically.

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

Produces a page of nested elements.

  | 
| [Walker_Comment::display_element()](https://developer.wordpress.org/reference/classes/walker_comment/display_element/)`wp-includes/class-walker-comment.php` |

Traverses elements to create list from elements.

  |

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

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

## User Contributed Notes

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