Page walker fixes from hailin. fixes #5498

git-svn-id: https://develop.svn.wordpress.org/trunk@6456 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren
2007-12-21 18:49:20 +00:00
parent f198d41f1b
commit 5ff262b3da
3 changed files with 3344 additions and 13 deletions

View File

@@ -465,33 +465,51 @@ class Walker {
if ($max_depth < -1) //invalid parameter
return $output;
if (empty($elements)) //nothing to walk
return $output;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
$flat = ($max_depth == -1) ? true : false;
if ( $flat ) {
// flat display
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$output = $this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
/*
* need to display in hierarchical order
* splice elements into two buckets: those without parent and those with parent
*/
$top_level_elements = array();
$children_elements = array();
foreach ( $elements as $e) {
if ( 0 == $e->$parent_field )
$top_level_elements[] = $e;
else
$children_elements[] = $e;
}
/*
* none of the elements is top level
* the first one must be root of the sub elements
*/
if ( !$top_level_elements ) {
$root = $children_elements[0];
for ( $i = 0; $i < sizeof( $children_elements ); $i++ ) {
$child = $children_elements[$i];
if ($root->$parent_field == $child->$parent_field )
$top_level_elements[] = $child;
array_splice( $children_elements, $i, 1 );
$i--;
}
}
foreach ( $top_level_elements as $e )
$output = $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );