how to properly list child pages in sidebar?

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 -->

Leave a Comment