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.
I thought this was interesting but as a learner you don’t explain what each bit of code does.