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]
Subscribe to:
Post Comments
(
Atom
)
Perfect! working in Wordpress 3.2.1
ReplyDeleteThank you.
ReplyDeleteGreat 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 :)
ReplyDeleteHey you can get that by inserting a statement into SQL query as LIMIT=3
ReplyDeleteI 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?
ReplyDeleteThanks.
can i insert a thumbnail, i tried but it doesn't work it only shows the "big" image. i used that code.
ReplyDelete