X

Unlimited number of child pages using wp_list_pages

Compact Archives Review

Adding this snippet to the sidebar.php of your wordpress theme will use wp_list_pages to display all child pages regardless of depth. This is something I have not found online and needed for a current theme I'm working on, enjoy!


( example screenshot )

Code Snippet:


if ($post->post_parent) {
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}

$children = get_pages('child_of='.$parent);
$child_pages = array(1);

foreach($children as $child) {
array_push($child_pages,$child->ID);
}
$all_pages = implode(",",$child_pages);

if( count( $children ) != 0 ) {
echo '<ul class="sidebar-navigation">'.
wp_list_pages( 'title_li=&sort_column=menu_order&echo=0&include='.$all_pages )
.'</ul>';
}

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!