I have a menu on a page that shows the children of a parent page, however, if a sub page is clicked on the children only of the sub page shows in this nav. is it possible to haver the direct children of the master parent page?

I'm currently using the following:

$thispage = $wp_query->post;

This is what I have used in a few projects that always displays the children pages even if you're are viewing a child page. See if this is what you had in mind

    // will display the subpages of this top level page
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    $ancestors = $post->ID;
    // diplays only the subpages of parent level
    //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
        // now you can get the the top ID of this page
        // wp is putting the ids DESC, thats why the top level ID is the last one
        $ancestors = end(get_post_ancestors( $post->ID ));
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        // you will always get the whole subpages list
if ($children) { ?>
    <div class="feature-box">
    <div class="feature-title">Related Links</div>
    <div class="subnav subnav-parent-<?php echo $ancestors; ?>">
            <?php echo $children; ?>
<?php } ?>
  • Sorry, just a quick one, how would I then wrap the child ul in a div?
    – James B
    Feb 20 '14 at 9:25
  • This is a low-quality answer. Instead of just adding copy-pasta code, please explain how/why the code would solve the OP's problem. Feb 20 '14 at 15:11

Your current logic is:

if we are a root page: ( we're 1 level deep! )
    show the children!
but if we are not a root page: ( we're 2 levels deep! )
    show the siblings/parents children!

And your issue is what happens when you arrive on a page that's 3 levels deep? We can check if we have a parent or not, but we also need to check if that parent has a parent too, aka a grandparent.

So this would do the trick:

if we are a root page: ( we're 1 level deep! )
    show the children!
but if we are not a root page: ( we're 2 or more levels deep! )
    get the parent post
    does the parent post have a parent too? AKA  a grandparent ( we're 3 levels deep! )
        show the grand parents children!
    but if the parent post does not have a parent ( we're 2 levels deep! )
        show the parents children!

But clearly this doesn't scale! What about 4 levels deep? 5 levels deep? What if my page has a parent, which has a parent, which has another parent? Too many levels! So confusing, and so many nested if else statements.

So how do we do this correctly?


For a truly robust answer, we need to use a recursive function. We don't know how many levels deep we are, so we can't account for every single level. Not without having huge if else statements and nightmarish code.

So instead, lets do something similar to this:

For post A
do the "show posts" routine to A

Where "show posts" is:

show posts:
    if A has no parent
        Display the children!!
    but if A DOES has a parent
        do the "show posts" routine on that parent

In more 'PHP-like' code: note, this is pseudocode, not actual code

function showPosts( $post ) {
    if ( $post->post_parent == 0 ) {
        // display the child posts
    } else {
        // go up one level and try again
        showPosts( $post->post_parent );
$this_page = $wp_query->post;
showPosts( $this_page );

Now we have something that will work for 1 level deep, 2 levels deep, 3 levels deep ..... 5000 levels deep, 5001 levels deep, etc etc etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.