X

How to Display the List of Child Pages of a WordPress Page

Snippets by IsItWP

Do you want to display the list of child pages of a WordPress page? Showing all your child pages help your readers easily navigate through other relevant pages. In this article, we’ll show you how to show child pages of a parent page using wp_list_pages.

Instructions:

All you have to do is add this code to your theme’s sidebar.php file. If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

	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>';
	}

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes or 7 best WordPress contact form plugins.

Comments   Leave a Reply

  1. I thought this was interesting but as a learner you don’t explain what each bit of code does.

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!