diff --git a/src/wp-includes/class-wp-walker.php b/src/wp-includes/class-wp-walker.php index df67921c2746c..01f47c1f7962b 100644 --- a/src/wp-includes/class-wp-walker.php +++ b/src/wp-includes/class-wp-walker.php @@ -252,15 +252,28 @@ public function walk( $elements, $max_depth, ...$args ) { $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); } + $has_excluded_parents = false; + + if ( $max_depth > 0 && isset( $args[0] ) ) { + if ( is_array( $args[0] ) && ! empty( $args[0]['exclude'] ) ) { + $has_excluded_parents = true; + } elseif ( is_object( $args[0] ) && ! empty( $args[0]->exclude ) ) { + $has_excluded_parents = true; + } + } + /* - * If we are displaying all levels, and remaining children_elements is not empty, - * then we got orphans, which should be displayed regardless. - */ - if ( ( 0 === $max_depth ) && count( $children_elements ) > 0 ) { - $empty_array = array(); - foreach ( $children_elements as $orphans ) { - foreach ( $orphans as $op ) { - $this->display_element( $op, $empty_array, 1, 0, $args, $output ); + * If we are displaying all levels (depth = 0), + * OR if specific parents were excluded with limited depth, + * we may have got orphans, which should be displayed regardless. + */ + if ( 0 === $max_depth || $has_excluded_parents ) { + if ( ! empty( $children_elements ) ) { + $empty_array = array(); + foreach ( $children_elements as $orphans ) { + foreach ( $orphans as $op ) { + $this->display_element( $op, $empty_array, 1, 0, $args, $output ); + } } } } diff --git a/tests/phpunit/tests/post/wpListPages.php b/tests/phpunit/tests/post/wpListPages.php index f059591219b1a..e81b8a5138a50 100644 --- a/tests/phpunit/tests/post/wpListPages.php +++ b/tests/phpunit/tests/post/wpListPages.php @@ -540,4 +540,36 @@ public function test_wp_list_pages_classes_with_hierarchical_cpt() { 'The output should contain exactly one "current_page_item" class.' ); } + + /** + * @ticket 27326 + */ + public function test_wp_list_page_combo_exclude_depth() { + $args = array( + 'echo' => false, + 'exclude' => self::$parent_3, + 'depth' => 3, + ); + + $expected = ''; + + $this->assertSameIgnoreEOL( $expected, wp_list_pages( $args ) ); + } }