Sep 2, 2011

Wordpress Show Excerpt Of Child Page | How To Show Excerpt Of Child Pages On Parent Page Of Wordpress

Today in this tutorial I am going to share a wordpress hack, which will show your child pages excerpts with title on the parent page.

OK, it was confusing, let me explain you.
Suppose you have  5 pages, which has a single parent page. If you want to show all the 5 sub pages on the Parent page, then you have to use this code.

Open your page.php file and paste this code after the loop.

[php]
<?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 ); ?>
<div style="border:1px solid #e7e7e7;margin: 10px;padding: 10px;">
<h2><a href="<?php echo  get_permalink($pageChild->ID); ?>" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
<?php
$your_custom_field = get_post_meta($pageChild->ID, 'your_custom_field', $single = true);
?>
<?php the_excerpt();?>
<p style="text-align:right;"><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>">Read More...</a></p>
</div>
<?php endforeach;
endif;
?>
[/php]

6 comments :

  1. Perfect! working in Wordpress 3.2.1

    ReplyDelete
  2. Thank you.

    ReplyDelete
  3. Great Post. It is working but I would like it to show only 3 of my 4 child pages at once on the parent page and show them randomly. I am not sure where to put the showposts=3 & orderby=rand ... Any help would be great :)

    ReplyDelete
  4. Niraj Chauhan19/9/11 9:58 PM

    Hey you can get that by inserting a statement into SQL query as LIMIT=3

    ReplyDelete
  5. I have certain pages "excluded" from my navigation with the WP excludes plugin. Is there a way to get the child pages to exclude those page marked as excluded too without having to add in each one manually?

    Thanks.

    ReplyDelete
  6. can i insert a thumbnail, i tried but it doesn't work it only shows the "big" image. i used that code.

    ReplyDelete