List children of second level sub page

Top Level Page
– Second Level Page
— Child Page
— Child Page
— Child Page

The goal here is to list all children of second level, I have that done from the codex. However, when I click on a Child, the children should still display because we’re technically still inside of the Second Level.

<?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
    ) );
?>

1 Answer
1

Is this what you are after?

<?php
    if($post->post_parent) {
        // if $post has parent than it is "Second level" and show its children.
        $children = wp_list_pages("child_of=".$post->post_parent."&echo=0");
    } else {
        // else it's a "Top level" so display children & grand children? 
        $children = wp_list_pages("child_of=".$post->ID."&echo=0");
    }

    if ($children) {
        echo "<ul>$children</ul>";
    }

Source: http://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage

Leave a Comment