WordPress Show title and excerpt of child pages on Parent page

When you want to display all the child pages title and excerpt of current parent page then you can display by adding following code in your page template file

<?php
$child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php/* And if you want to get the custom field values then you can get that by */
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
the_excerpt();?>
<?php endforeach; endif; ?>

If you don’t want to display child pages on every page then you can use WordPress Conditional Tags.

34 thoughts on “WordPress Show title and excerpt of child pages on Parent page

  1. How can I use de plugin? It’s activated and options ok… but it dón’t see what’s the code to insert in a post

    1. @Juanjo
      Sorry right now there are no short-codes available to insert the slideshow in the post page or widget area.
      If you want to display the slidee in your theme then you have to add this code.

      <?php include (ABSPATH . '/wp-content/plugins/content-slide/slideshow.php'); ?>
      

      in your theme .php files where you want to show that slider.

  2. Hi

    First of all great plugin very decent style and different from others as well.

    I have placed on my website and its working fine but my top navigation’s dropdown not working with this plugin.. I’m using this plugin for page navigation WP-PageNavi.

    Is there anything can i do and it will fix ?

  3. It has been solved. actually there was already called jquery in the header and this plugin calling it self another jquery file which is conflicting with each other. Just removed the plugin file and its working fine 🙂

  4. @Michael Caputo
    Use

    $your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
    

    within the loop to get the custom field values.

  5. I setup the code on my page template file. It works, but the thumbnail is of the parent page and not of the child pages. How can I display the child page thumbnails next to the child page excerpt.

  6. Hey, great snippet! Is there a way that this line `$pageChild->guid` can be changed to respect the Permalink settings. Currently it gives out the raw link, not a pretty one. Thanks!

  7. Awesome trick, but is there a way we can get the child page’s image? I’ve been trying with , but all I get is a repeated display of my current page’s image.

    Thanks again.

  8. Hi!
    Thx for this code.
    But.. i got one problem.. when have more than 1 level of sub-pages, the excerpt that show is of the first child page. Ex:
    – Top page
    – – Child 1
    – – – Child Child 1
    – – – – Child Child Child 1

    At the second level, instead shows the excerpt of “Child Child Child 1”, shows the “Child 1” Excerpt.

    Got it? Anyway to solve this problem?

    Thanks

  9. thanks for posting this, just what I needed! 🙂

    just have to work out how to show the image in miniature using custom fields now.

  10. Fantastic script. Ty. After way to many hours yours works. Can you show a beginner how to fine tune the code to add the pic and maybe create a link from the title to the full page? Again thank you for helping make better blogs for all

    1. CJ: would you be so kind as to share what you mean by “I threw in the old get thumnbnail” You are right it would make it even awesomer. I use reviewazon and it would be even much more awesomer if I could figure out how to get the script to pull excerpts from the description areq rather then the page content area.
      Thank youu for any help you may have to share.

      Dave

      1. @DaveT
        if you want to get post thumbnail which is added after wordpress 2.9 then you have to add

        <?php the_post_thumbnail(array(250,150)); ?> 
        

        so final code will be

        <?php
        $child_pages = $wpdb->get_results("SELECT *	FROM $wpdb->posts WHERE post_parent = ".$post->ID."	AND post_type = 'page' ORDER BY menu_order", 'OBJECT');	?>
        <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
        <h2 class="subpagetitle"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
        /* And if you want to get the custom field values then you can get that by */
        $your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
        <?php the_post_thumbnail(array(250,150)); ?> 
        <?php the_excerpt();?>
        <?php endforeach; endif;
        ?>
        

        For the Excerpt part if you want to get excerpt from the “Excerpt Box” then it is displaying content from excerpt area. and if excerpt box is blank then it displays content from page.

  11. Thanks for the code guys really useful however I can’t get the code to display my ‘thumbnail’ image in the list. You can view the site here…

    Any ideas?

  12. Hi there, this is a great post! All I am wondering is how I would make it show a random post and show only 3 of the parent pages at once. If this could be answered I would be really grateful

    1. just change following line

      $child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); 
      

      with this one

      $child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY RAND() LIMIT 3", 'OBJECT'); 
      
  13. Thanks for the code, now I just got to figure out how to use it with the theme I’m using which is nearly done and then the styling.

  14. I basically know nothing about php. Can someone please tell step by step what to do?

    Do I have to put the code on page.php? If so which part of the existing code?

    I tried to put it on the page but it is not displaying correctly.

    Thanks

Leave a Reply

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