1

On hierarchal pages 3 levels deep, I would like to display the grandchildren on child and grandchild pages (but not on the parent page). What I have so far displays the grandchildren on the child page but I'm confused on how to keep them displayed on the grandchildren pages. How can I do this?

         <?php
            //display children's child pages
            $childrens_children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');

            if ( is_page() && $post->post_parent > 0 ) { 
            ?>
            <aside id="child-child-pages" class="widget widget_child_child_pages">
                <ul> 
                    <?php echo $childrens_children; ?>
                </ul>
            </aside>
        <?php } ?> 

The idea

Parent : show nothing

Child : show grandchildren of parent

Grandchild : show grandchildren of parent

2

So if I have this straight:

IF We are currently viewing a Top Level Page THEN Show Nothing

IF We are currently viewing a Child Page THEN Show Grandchildren

IF We are currently viewing a Grandchild Page THEN Show Grandchildren

If the above logic is correct we can use something like this:

$id = get_ancestor(); // Hold Top Most Page ID

<?php if($post->post_parent != 0) : /** IF we're NOT on a Top Level Page **/ ?>
    <ul>
        <?php 
            $parentID = ($post->post_parent == $id) ? $post->ID : $post->post_parent;
            wp_list_pages("child_of=$parentID&depth=1"); 
        ?>
    </ul>
<?php endif; ?>

Put this into your functions.php file, it's a function that will return our top most page ID.

/** Get Top Most Page ID and Return it **/
function get_ancestor(){
    global $post;
    $id = 0;

    if(is_object($post)){
        $id = $post->ID;

        if($post->post_parent){
            $ancestors = get_post_ancestors($post->ID);
            $root = count($ancestors)-1;
            $id = $ancestors[$root];
        }
    } 
    else if(is_singular('post') || is_archive() || (is_home() && !is_front_page())){
        $id = get_option('page_for_posts');
    }

    return $id;
}
  • I received an error when trying this bit. But actually, I'm trying to: IF We are currently viewing a Top Level Page THEN show NOTHING. IF We are currently viewing a Child Page (of top level) THEN Show Children of top level. IF We are currently viewing a Grandchild Page (of top level) THEN Show Children of top level. Hope that makes more sense. – gstricklind Jun 30 '14 at 19:36
  • I've edited my answer to reflect your conditionals. – Howdy_McGee Jul 2 '14 at 14:27
  • Oh dear, I seem to have confused the both of us. Apologies. The top level part works great. But I'm trying to get the child page (of top level) to show it's children (parents grandchildren). And the grandchild pages (of top level) to show thier sibblings (grandchild of top level) - this works perfectly as well. Apologies again for the misunderstanding in my previous comment. I've also corrected your call for the $id – gstricklind Jul 2 '14 at 16:14
  • Great! If it works, don't forget to mark as answered. Otherwise if you have any questions let me know! – Howdy_McGee Jul 2 '14 at 16:37

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.