Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My pages menu is maximum 3 levels deep and I am having a hard time figuring out how to properly list the child pages in the sidebar.

If a page is a parent I want to list it's direct child pages and the title should be this parent.

If a page is a child AND has children of itself, I want to list only it's children and the title should be this page

If a page is a grandchild, I want to list it's siblings and the title should be this grandchild.

For now I am using (from the Codex):

<?php if($post->post_parent) {
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1&sortcolumn=menuorder");
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1&sortcolumn=menuorder");
}
?>
<?php if ($children) { ?>
<div class="left-bar-content">
<h2><?php _e('More ','sunchine') ?><?php echo get_the_title($post->post_parent);?></h2>
<ul class="leftbar-list">
<?php echo $children;?>
</ul>
</div><!-- .left-bar-content -->
<?php
}
?>

And this is almost correct. Almost, because my it doesn't do my 2nd criterium.

The Codex has heaps of options, but I cannot figure out which would be applicable to what I want to accomplish. Using widgets and/or a plugin is not an option.

Any help is much appreciated.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Here is the code that satisfies all your 3 requirements above.

<?php
/*
 * get_page_depth
 * Gets the page depth, calls get_post on every iteration
 * https://gist.github.com/1039575
 */
if ( !function_exists( 'get_page_depth' ) ) {
function get_page_depth( $id=0, $depth=0 ) {
    global $post;

    if ( $id == 0 ) 
        $id = $post->ID;

    $page = get_post( $id );
    if ( !$page->post_parent ) {
            // this page does not have any parent
            return $depth;
    }
    return get_page_depth( $page->post_parent, $depth+1 );
}
}

$target_page = get_page_depth( $post->ID ) > 1 ? $post->post_parent : $post->ID;
$children = wp_list_pages("title_li=&child_of={$target_page}&depth=1&echo=0&sort_column=menu_order");

?>

<div class="left-bar-content">
    <h2><?php _e('More ','sunchine'); ?><?php echo get_the_title( $target_page ); ?></h2>
    <ul class="leftbar-list">
        <?php echo $children; ?>
    </ul>
</div><!-- .left-bar-content -->
share|improve this answer
    
this is almost perfect indeed. The only thing that does not go correct is when on a grand child, it should show the title of the direct parent, not the the title of the grandchild (which it shows now) and not the title of the ancestor. So basically when the title is echoed, it should be $post->ID for criteria 1 and 2, but $post->post_parentfor criterium 3... The output in the sidebar is already what it should be so many thanks for that already! –  Piet Jun 22 '11 at 7:39
    
fix the code, note the change in get_the_title. It should now show parent title on grandchild page. –  Hameedullah Khan Jun 22 '11 at 8:11
    
Khan, yes Sir! Thank you so much, this total works! And sorry, I re-read my original question and I think I mixed it up myself about the 3rd criterium... –  Piet Jun 22 '11 at 8:17
    
@Piet, you are always welcome. :) –  Hameedullah Khan Jun 22 '11 at 8:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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