Lets say I have this menu:
Top
|___ Sub 1
|___ Sub Sub 1
|___ Sub Sub 2
|___ Sub Sub 3
|___ Sub Sub 4
|___ Sub 2
|___ Sub Sub 1
|___ Sub Sub 2
|___ Sub Sub 3
|___ Sub Sub 4
|___ Sub 3
|___ Sub Sub 1
|___ Sub Sub 2
|___ Sub Sub 3
|___ Sub Sub 4
|___ Sub 4
|___ Sub Sub 1
|___ Sub Sub 2
|___ Sub Sub 3
|___ Sub Sub 4
I can list that menu using this:
if( 0 == $post->post_parent && 0 == count( $children ) )
return;
if( 0 == $post->post_parent )
{
$child_of = $post->ID;
}
else {
$parents = get_post_ancestors( $post->ID );
$child_of = end( $parents );
}
$args = array
(
'child_of' => $child_of,
'echo' => 0,
'title_li' => ''
);
$pages = wp_list_pages( $args );
This is fine but I don’t want to show all page items. What I want is that only imediate children of the page you are on is shown.
So if I’m on page Top
the menu should appear like so:
Top
|___ Sub 1
|___ Sub 2
|___ Sub 3
|___ Sub 4
If I’m on page Top/Sub 3
the menu should appear like so:
Top
|___ Sub 1
|___ Sub 2
|___ Sub 3
|___ Sub Sub 1
|___ Sub Sub 2
|___ Sub Sub 3
|___ Sub Sub 4
|___ Sub 4
And so on, so that it can work to any depth.
Rarst put up a nice answer but this was for when using WordPress menus. I’m looking for the same for wp_list_pages(). Looking for an answer that uses a Walker or filters/hooks. I know how to solve this problem with CSS but this doesn’t fix the problem of unnecessary HTML being sent to the browser.
I have come up with my own solution but I’m not convinced it’s the best.
Walker:
You can consider using a mixture of get_children() for the children of the page and page siblings and get_post_ancestors() for the ancestors:
Edit – a quick demonstration of the idea which can use some clean up:
Considere use the argument “depth”
Example: