WordPress Get List Of Child Pages or List Sub Pages Of Current Page

Hi Friends, WordPress is widely used blogging platform but these days it is used to make CMS and other sites too, So many times developers have to make lots of customization or make new code to achieve clients requirements and this is reason I am writing this article. One of my client have requirement in which when user click on any page then user can see list of sub pages or child pages with sub-page title with link, sub-page content as excerpt and read more link. May be there are plugins for this but I really don’t like to use plugin for each requirement. So I made code for it which I am going to explain.

Now Lets take example to understand it easily. You have one parent page “Services” and “Services” have sub pages “WordPress Freelancer” , “WordPress Developer India”, “Web Design” and “PHP Freelancer”. So “Services” having 4 sub pages. Now via custom menu you can make pages and sub pages drop down menu, but as I told you we need to work like when any user click on “Services” then it should show its sub pages on it. Below is code for it.


<?php
$postid = get_the_ID();
$mypages = get_pages( array( 'child_of' => $postid, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
foreach( $mypages as $page )
{
 $content = $page->post_content;
 $content = apply_filters( 'the_content', $content );
 ?>
 <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
 <?php echo content($content); ?>
 <a class="button" href="<?php echo get_page_link( $page->ID ); ?>" target="_self">+ info</a>
<?php
}//End of foreach
?>

Line by line explanation :
Line 2 : get current page ID.
Line 3 : Use get_pages wordpress function to get child pages/sub pages of current page by passing id(id which we get in Line 1) in it.You can see passed $postid in get_pages function.It will return all sub pages in $mypages variable.
Line 4 : Start foreach loop to list sub pages.
Line 6 : To get content of each sub page.
Line 7 : Apply the_content filter.
Line 9 : Print title of sub page and Assing link to it.
Line 10 : Echo content by content function. This is not wordpress function It is made by me and given below, you just have to copy paste it in functions.php file of your activated theme. This function is to remove image blackquote and other elements from content.

<?php
function content($content) {

$theContent = $content;
 $num=40;
 $output = preg_replace('/<img[^>]+./','', $theContent);
 $output = preg_replace( '/<blockquote>.*<\/blockquote>/', '', $output );
 $output = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $output );
 $limit = $num+1;
 $content = explode(' ', $output, $limit);
 array_pop($content);
 $content = implode(" ",$content)."<br /><br />";
 echo $content;
}
?>

Line 11 : Make read more link for sub page. In code there is + info text on which we made link, You can change it as you want.
Line 13 : End of foreach loop.

So this is code to list childpages or sub pages of current page in wordpress, I hope it will help someone. If you know any other code which is easier than this, then you can post in comment.

I am wordpress DeveloperWordPress Developer India and WordPress Freelancer. If you have any projects related to wordpress or PHP you can contact me.

4 Comments

  • crane May 7, 2013 Reply

    hi,
    Could you guide how to display ralated child pages which are in the same parent page?

  • Alberto November 13, 2013 Reply

    Nice tutorial mate 😉 thanks !

  • Dalveer Nayak November 28, 2013 Reply

    dheek hai

  • ashok98 March 12, 2015 Reply

    Good………………

Leave a Reply

Your email address will not be published. Required fields are marked *