WordPress Show title and excerpt of child pages on Parent page

Home » Blog » Resources » Wordpress » Wordpress Tips and Tricks » 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.

42 Comments on "WordPress Show title and excerpt of child pages on Parent page"

  1. Juanjo says:

    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

    Reply →
    • snilesh says:

      @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.

      Reply →
  2. I have used News ticker. But my boss want to display a page that contain all the post from new ticker. Is it possible?

    Reply →
  3. Babar says:

    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 ?

    Reply →
  4. Babar says:

    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 :)

    Reply →
  5. Thank you that’s really helped me out. I was struggling with http://wpguru.co.za/templates/page/display-title-excerpt-of-child-pages/ but this worked a treat!

    Reply →
  6. argrone says:

    I can activate the plug-in however, when I go to save the settings I am getting a 500 Internal Server error… any ideas?

    Reply →
  7. Hey there, this worked great for me.

    Any chance you could share how we can access custom fields within the loop?

    Reply →
    • snilesh says:

      @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.

      Reply →
  8. Thanks for responding so quickly!

    I’ve got the custom fields working, now i’m trying to get the links to the sub pages working. the

    <?php echo $pageChild->guid; ?>

    doesn’t seem to want to link to the sub page.. Any help?

    Reply →
  9. Got it

    echo $pageChild->guid;
    Reply →
  10. Marco says:

    Where are the screenshots? I can’t see them..

    Reply →
  11. Anthony says:

    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.

    Reply →
  12. Drew says:

    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!

    Reply →
  13. Carey Best says:

    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.

    Reply →
  14. Fábio ZC says:

    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

    Reply →
  15. Fábio ZC says:

    finally everything is ok..
    i changed

    by
    post_excerpt;?>

    Reply →
  16. Guy says:

    thanks for posting this, just what I needed! :)

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

    Reply →
  17. DaveT says:

    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

    Reply →
  18. CJ says:

    Awesome post! Keep it up! I threw in the old get thumbnail to make it even awesomer.

    Reply →
    • DaveT says:

      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

      Reply →
      • snilesh says:

        @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.

        Reply →
        • DaveT says:

          Snilesh: Thank you so much for the code. I have not be able to get it to work yet but I plan to spend some serious time with over the weekend. I tried loading the entire code as you show ti and when I do i lose my entire sidebar and get an error when I try to go to the parent page. I will report back after I have sat and played for awhile. In the mean time thank you again for your help
          ENJOY THE DAY

          Reply →
  19. jamal says:

    this work is very very importent for me. thanks

    Reply →
  20. Peter says:

    You are a lifesaver – thanks for the tutorial, worked a charm.

    Reply →
  21. shag says:

    EXCELLENT!!! been searching for a while now. other solutions weren’t working easily with custom post types like this one does. Thanks.

    Reply →
  22. ShigueS says:

    Thank you. It helped a lot!

    Reply →
  23. richard says:

    Just what I was looking for!!!

    Thanks a lot!

    Reply →
  24. Stephen says:

    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… http://www.shorestreet.org/ministries/youth-childrens

    Any ideas?

    Reply →
  25. flashuac says:

    Very interesting post! :)

    Reply →
  26. Ryan says:

    This code works perfect! Is is possible to exclude a certain child page from displaying?

    Reply →
  27. Steve says:

    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

    Reply →
    • snilesh says:

      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'); 
      
      Reply →
  28. sabrina says:

    Thanks for the codes for this. Any way there’s a plugin that can install the code itself?

    Reply →
  29. Horsham says:

    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.

    Reply →
  30. Klixindia says:

    This is a great article.. Before finding this i was searching over google from last one week…

    Reply →
  31. gezgin says:

    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

    Reply →
  32. This is really a source for our development for WordPress. Glad that I had found this simple tips.

    Thanks a lot..

    Reply →

Trackbacks for this post

  1. »

Got something to say? Go for it!

CommentLuv badge