Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#8150 closed defect (bug) (fixed)

walker class special case

Reported by: hailin's profile hailin Owned by:
Milestone: 2.7 Priority: normal
Severity: normal Version: 2.7
Component: General Keywords: has-patch, tested, reporter-feedback
Focuses: Cc:


In function walk( $elements, $max_depth),
when none of the elements is top level (parent = 0)
we assume the first one must be root of the sub elements.
and get it as $root = $elements[0].

This implicitly assumes that the first index is 0.
In fact, in some cases, it can start with any integer.
It's observed that it can start with 2. When that happens,
walker will not construct any top level elements, end up treating
all as orphans.

The better and safe, way is to user array function:

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

Attachments (1)

8150_walker.diff (523 bytes) - added by hailin 15 years ago.

Download all attachments as: .zip

Change History (6)

15 years ago


#1 @lloydbudd
15 years ago

  • Keywords has-patch tested added
  • Milestone changed from 2.8 to 2.7
  • Version set to 2.7

#2 @westi
15 years ago

  • Keywords reporter-feedback added

The patch looks good.

It would be good to know what the user problem for this issue is though.

What can it affect the display of?

#3 @hailin
15 years ago

The issues is that with calls such as:


It will produce array $elements (categories), and there is not a single element whose parent=0 since they are children of 400052.

Then it will be handled by:


  • when none of the elements is top level
  • assume the first one must be root of the sub elements */

if ( empty($top_level_elements) )


Because $elements is an associative array, the key may not begin with 0.
I've observed a bug on a particular blog, in which case the first element's key is

  1. That caused the $root= $elements[0] to be NULL since the key 0 does not exist.

As a result, $top_level_elements are empty, and then every element is displayed as orphans. The correct results would be nested structure.

With this fix, they are displayed correctly. It's tested in both positive and negative cases.

#4 @westi
15 years ago

Cool. Thanks for that hailin.

I like tickets to document the customer/end user issue so that I can understand how to test the fixes and if the issue is reported again I can easily refer back and say fixed by this.

#5 @ryan
15 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [9685]) Walker fix for when non elements are top-level. Props hailin. fixes #8150

Note: See TracTickets for help on using tickets.