I am using wp_list_pages to show the page structure on a website. For instance, if I use this code, it will show me the complete child / grandparent heirarchy of the current page the user is on:


So this would generate something like this: (keep in mind, the user is on a page called About, which is the parent)

enter image description here

This works great, but if you were to click on any grandparent page (Like Partners), the wp_list_pages code does not work. I have implemented a conditional statement which will use different code on a child page then the grandparent page. So that said, I have the child pages working great. I just need to figure out how to display this same exact menu on the grandparent pages.

Any idea?

Here is how the conditional work:

<?php if( count(get_post_ancestors($post->ID)) == 2 && $post->post_parent){ ?>

I need new code here

<?php } elseif(count(get_post_ancestors($post->ID)) == 1 && $post->post_parent){
        $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
        echo $children;
else { ?>
    <li>Parent Page</li>
<?php } ?>
  • Can you post your conditional for showing it on the child pages?
    – NW Tech
    Commented Jan 17, 2013 at 21:49

1 Answer 1


In the area above where it says "I need new code here" I simply grabbed the ID using get_post_ancestors like so:

$parent = array();
$parent = get_post_ancestors($post->ID);
$lastNum =  end($parent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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