Walker::display_element( object $element, array $children_elements, int $max_depth, int $depth, array $args, string $output )
- Since
- 2.5.0
- Source
wp-includes/class-wp-walker.php:133
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.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$elementobject- Data object.
$children_elementsarray- List of elements to continue traversing (passed by reference).
$max_depthint- Max depth to traverse.
$depthint- Depth of current element.
$argsarray- An array of arguments.
$outputstring- Used to append additional content (passed by reference).
Performance profile
How much work a call to Walker::display_element() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 8–72
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Walker::display_element() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | none |
| always | 47–63 | array_values(), ->start_el(), array_values(), ->end_el() |
isset($newlevel) | 57–72 | array_values(), ->start_el(), array_values(), ->end_lvl(), array_values(), ->end_el() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 95 | 8–72 | 11 | |
| 8.5 | 95 | 8–72 | 11 | |
| 8.4 | 95 | 8–72 | 11 | |
| 8.3 | 95 | 8–72 | 11 | |
| 8.2 | 95 | 8–72 | 11 | |
| 8.1 | 95 | 8–72 | 11 | 4 more instructions than PHP 7.4 |
| 7.4 | 91 | 8–69 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- Walker::start_el()Starts the element output.
- Walker::start_lvl()Starts the list before the elements are added.
- Walker::display_element()Traverses elements to create list from elements.
- Walker::end_lvl()Ends the list of after the elements are added.
- Walker::end_el()Ends the element output, if needed.
Used by · 4
- Walker::display_element()Traverses elements to create list from elements.
- Walker::paged_walk()Produces a page of nested elements.
- Walker::walk()Displays array of elements hierarchically.
- Walker_Comment::display_element()Traverses elements to create list from elements.
Source code
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 ) ); }Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-walker.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.