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

Traverse elements to create list from elements.


Description 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 Parameters

$element

(object) (Required) Data object.

$children_elements

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

$max_depth

(int) (Required) Max depth to traverse.

$depth

(int) (Required) Depth of current element.

$args

(array) (Required) An array of arguments.

$output

(string) (Required) Used to append additional content (passed by reference).


Top ↑

Source Source

File: wp-includes/class-wp-walker.php

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

		$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 childrens for this element
		if ( ( $max_depth == 0 || $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 ) );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.