Display title & excerpt of child pages

Image by: moonjazz
When you’re actively using the page hierarchy in WordPress, many times you’ll create a parent page simply so that you can create the sub pages. For example, a page called Team might only be created so that you can have Joe Soap and Jane Doe as sub pages. There’s no real content to be put on that parent page, because the child pages are where the real information lies.
Instead of simply typing “Click the links to the left” and leaving the page largely empty, you can create a dynamic page template that will display a title and excerpt for each of the child pages.
You can do this by inserting this below the main loop of your page.php file.
<? $pageChildren = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<? if ( $pageChildren ) : foreach ( $pageChildren as $pageChild ) : setup_postdata( $pageChild ); ?>
<!-- loop stuff here -->
<? endforeach; endif; ?>
In cases where you actually do have content for your parent page and you don’t want to display this title/excerpt setup, then you can make use of conditional tags to exclude the posts where you don’t want this to happen. For example:
<? if (is_page('3')) { } else { // if its page 3 then show nothing ?>
<? $pageChildren = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<? if ( $pageChildren ) : foreach ( $pageChildren as $pageChild ) : setup_postdata( $pageChild ); ?>
<!-- loop stuff here -->
<? endforeach; endif; } ?>



hi there.
this is EXACTLY what i’m looking for, so thanks for the post.
i can’t get it to work though – can you elaborate a little on it’s implementation (i’m no php Guru).
Can you explain what you’ve done thus far Craig?
This query (the first of the two) must be placed on a page.php template, before your normal page loop…
Have you done this?
I’m also having trouble getting this going, although it sounds like exactly what I need. I’m not sure exactly what the loop stuff is. Here’s my code:
get_results(“SELECT * FROM $wpdb->posts WHERE post_parent = “.$post->ID.” AND post_type = ‘page’ ORDER BY menu_order”, ‘OBJECT’); ?>
<!–
–>
Sorry about that mess of a post. Basically, I’ve put your code in my template file after
“”
and before
“”
Not sure where to put
“”
This is brilliant! I can get the excerpt to show up… but when I use the ´the_title’ template tag is shows the parent page title instead of the child page title… what am I doing wrong?
@Jan Eg – I was having this problem too. Instead of “the_title” try this:
echo $pageChild->post_title;
Now I’m trying to figure out how to output the link to the child page… the_permalink gives me a link to the parent page
Aha! Here’s how you get the url for the child page. Put it into an anchor tag/href:
echo get_permalink($pageChild->ID);
Wonderful! That does the trick. Wouldn’t it be wonderful if WordPress had this as an option in core?
This is the code I put in my page.php file:
get_results(“SELECT * FROM $wpdb->posts WHERE post_parent = “.$post->ID.” AND post_type = ‘page’ ORDER BY menu_order”, ‘OBJECT’); ?>
<a href=”ID); ?>” rel=”bookmark” title=”Permanent Link to “>post_title; ?>
Is it possible to post php in a comment?
hey, this is exactly the sort of thing I was looking for when using WP as a simple CMS. I’m no PHP guru though and although I got the code into a template page without any errors (i.e. below the loop) it’s not pulling up the child page excerpts on the parent page. Is there a silly error I could have done (i pasted you non-conditional tag below the loop BTW) Thanks!
Tried again above the loop (second comment contradicts the article where it says below?) and still no children, any thoughts? thanks in advance!
Hello Patricia,
The commented “loop stuff here” should be replaced with:
< a href=“ID); ?>“ rel=”bookmark” title=”Permanent Link to “> post_title; ?>
Here is a link to what the page.php file of the default theme looks with this code: http://www.norskkurs.nl/jv/?page_id=3
@Jan Eg – Wow, a Huge and Heartfelt thanks to you for going to the trouble to clarify this for me – I was miles off and would have never got it working on my own – the SRC code you posted was extremely helpful and low and behold its working. It’s a pretty powerful script to get these features works, so again a huge thanks to you!
@Jan Eg – yeah, ditto. thanks, I’ve been looking for something like this for a while now.
I have the same question as Patricia, who’s first questions didn’t really seem to get answered:
I want to display an excerpt of the child page content on the parent page.
I’ve tried replacing the “loop stuff here” with the_content() and adding a ‘more’ quicktag to the child content but this doesn’t work. I’m either getting all or nothing.
@shalako – you can check out the generous posting from Jan Eg above, that’s what did it for me. If I recall, I then substituted the_content() with the_excerpt() and by using an excerpt plugin, it will make a nice display of child page entries keeping the HTML / Images of the child page entries.
Finally got this working in combination with another plugin. http://wordpress.org/extend/plugins/improved-include-page/installation/.
After installing this plugin, I replaced “loop stuff here” with
echo iinclude_page($pageChild->ID,’DT_TEASER_MORE’);
There is a way to do this using built-in wordpress functions:
ID);
$more = 0;
while (have_posts()) : the_post();
?>
//You can call regular wordpress functions here (ie. the_title() and the_content();)
Let’s try to again but with extra spaces in the php opening and closing tags:
ID);
$more = 0;
while (have_posts()) : the_post();
? >
//You can call regular wordpress functions here (ie. the_title() and the_content();)
Yeah that is true. It’s much better. I use Theme Hybrid’s great get_the_image function to show thumbnails for pages. But I use the next peace of code now:
$project_list = get_posts(‘numberposts=5&order=ASC&orderby=menu_order&post_type=page&post_parent=’.$post->ID);
foreach($project_list as $post):
setup_postdata($post);
The code above also works with the regular wordpress template tags.
Hi Mark
thanks so much for sharing this – its EXACTLY what i was looking for – after reading this post i implemented it within a few minutes. Great to have WordPress help on our doorstep here in SA
Hej, I’ve been trying to implement this one, but can’t get it work.. Am I doing something wrong maybe..? Or is it not supported in wordpress 2.7?
I’ve copied the code exactly and placed it belove the_post()
Here is what I did. This creates a list of the sub-page titles, as a link to the sub-page, with the excerpt below.
Thanks so much for the solution.
I’m a noobie, but pretty pleased with how I got the pages to list, stopping at the tag and then including a link to the page. Here’s my code:
get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?><a href="ID); ?>" rel="bookmark" title="Permanent Link to post_title; ?>">post_title; ?>
<a href="ID); ?>" rel="bookmark" title="Permanent Link to post_title; ?>">Read more about post_title; ?>
Let me retry that (found online code converter at http://www.tutorialtastic.co.uk/converter.php ):
Thanks so much for the solution.
I’m a noobie, but pretty pleased with how I got the pages to list, stopping at the <!–more–> tag and then including a link to the page. Here’s my code:
<? $pageChildren = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = ‘page’ ORDER BY menu_order", ‘OBJECT’); ?>
<? if ( $pageChildren ) : foreach ( $pageChildren as $pageChild ) : setup_postdata( $pageChild ); ?>
<!– loop stuff here –>
<h3><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="Permanent Link to <? echo $pageChild->post_title; ?>"><? echo $pageChild->post_title; ?></a></h3>
<?php $more = 0; the_content(”,true); ?><a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="Permanent Link to <? echo $pageChild->post_title; ?>">Read more about <? echo $pageChild->post_title; ?></a>
<? endforeach; endif; ?>
Hi, thanks for this code! How can I check to see if there are subpages, and if not, to just show the regular page.
For example, if I have Page 10 that lists all subpages, but first I want to check if page 10 has subpages, and if not,to just show Page 10. I would be so thankful for any advice.
You simply answered the question!! Thanks a lot. I needed this sub page excerpt thing for the latest website I am working on. Thanks again
Thanks for the code – not so readily available as one might think…
My question – when “pulling” the subpage content, it seems that WP core code is not rendered correctly. Such as a wp gallery in the subpage or a wp caption to a photo.
Any idea how to sort this out?
Some of these I would have never have thought of. ,
The Magliozzis previously took a break at approximately the half-hour mark of the show. ,
I’ve implemented the first block of code and I’ve changed the_excerpt to the_content and everything is working great, so thats for that. However, this might just be a caching problem but when I have a page in draft it still gets pulled out and displayed on the parent page. Is there a way around this?
Try this:
` ‘page’,
‘meta_value’=> ‘voorpagina’,
‘order’=>’ASC’,
));?>
`
— the_title and the_content here —
It may be worth adding a reminder that you’ll need to add:
global $wpdb;
at the start of any custom function that attempts to use the code – otherwise it’ll either just silently fail, or more likely, die at the point of the query.
Otherwise, thanks for sharing!
@JanEg
Your code works perfectly!
By the way, I’m able to pull in custom field values as well as regulary wordpress loop stuff…
Nice!
Thanks a lot – works fine now!
Just a few notes from me:
1. When you copy and past the code from this site, you may – like me – have troubles because of the special signs in the code:
get_results(”SELECT * FROM $wpdb->posts WHERE post_parent = “.$post->ID.” AND post_type = ‘page’ ORDER BY menu_order”, ‘OBJECT’); ?>
To solve the error in execution, I replaced the
“ by ” and ‘ by ‘
e.g. ”SELECT becomes “SELECT
e.g. ‘page’ becomes ‘page’
- and then it worked!
2. To correct the SELECT statement to only search for published pages, after
AND post_type = ‘page’
add this:
AND post_status = ‘publish’
It’s me again.
I see now, that the signs are converted on this site, so that you don’t see any difference, but maybe you’ll pay attention to using the correct “ ” and ‘ ‘
…
I was having problems getting this to work and found an alternative at http://www.snilesh.com/resources/wordpress/wordpress-hacks-and-tricks/wordpress-show-title-and-excerpt-of-child-pages-on-parent-page
Hope that helps
I have used this several times on different sites and it is great. Right now I stuck and hope someone can help. There are published child pages I need to exclude from listing on the parent page.
Can anyone tell me how to do this?
If there are many pages, how can I do to display only 10 per page, as is the case of posts? Or you can use wp_pagenavi?
Thanks in advance!
I’ll try Gabe Jamison’s technique for generating the following pages.
I got child page from this code
get_results(“SELECT * FROM $wpdb->posts WHERE post_parent = “.$post->ID.” AND post_type = ‘page’ ORDER BY menu_order”, ‘OBJECT’); //echo “
Perfect, thank you very much! Trying to nest pages in a sub page but use a parent template for dynamic div loading or new pages.
Great one. I tried it and got the result.
Thanks for sharing such a useful stuff.
Very useful post. I tried it and is very interesting and impressive.
I really enjoyed visiting your blog.
Thanks
Great code! It works great but is there a way to exclude certain pages? Some of the children are categorized under the parent but users have no reason to access the pages without a direct link. Please let me know if I can do this.
Thanks!