This should work, using nothing more than the available argument-array parameters for wp_list_pages()
: specifically, depth
and child_of
.
To display one level of hierarchy, for descendant pages of the current page:
<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
wp_list_pages( array(
// Only pages that are children of the current page
'child_of' => $post->ID,
// Only show one level of hierarchy
'depth' => 1
) );
?>
There should be no need to resort to a custom walker class.
EDIT
TO show the top-level links as well, simply change a couple of the parameters:
<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
wp_list_pages( array(
// Only pages that are children of the current page's parent
'child_of' => $post->post_parent,
// Only show two level of hierarchy
'depth' => 2
) );
?>
Note the change of 'child_of' => $post->ID
to 'child_of' => $post->post_parent
, which will include pages of the current page’s parent page, and the change of 'depth' => 1
to 'depth' => 2
, which will include the current page’s siblings, and their children.
EDIT 2
Okay, here’s how I would handle the code you just added. First, I would use a proper array, rather than an array string. Then, I would query for context/child-pages before building the wp_list_pages()
argument array, then, just call wp_list_pages()
once:
// Use parent page if exists, else use current page
$child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
// Depth of 2 if parent page, else depth of 1
$depth_value = ( $post->post_parent ? 2 : 1 );
// Build argument array
$wp_list_pages_args = array(
'child_of' => $child_of_value,
'depth' => $depth_value,
'title_li' => 'Sections'
);
// Now, output the result
wp_list_pages( $wp_list_pages_args );
We’ll need more complex code, if you actually need to output separate lists.