Display Child Pages title & content on Parent Page
I am developing a custom theme for my client, and there was a need to show the title and the content of the child pages under the specific page within the website.
I’ve been looking for a solution for a few days already and every solution iv’e found did the same thing, it posted the titles of the child pages but the content under these titles was the content of the current page i have been on.
Eventaully i have made some adjustments on a code i have found on a wordpress support forums and it worked perfectly.
Here is the code:
<?php
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($pages as $page)
{
$content = $page->post_content;
?>
<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
<p><?php echo $content ?></p>
<?php
}
?>This way the page that you are on will show in a post type the child pages of that page.
5 Responses to Display Child Pages title & content on Parent Page
I used this code and I ended up losing all the return carriages in the content. I can’t seem to figure out where it is all falling to pieces. The wordpress entries are normal, but when they post on the page… they become one giant paragraph.
replace line 09 above () with:
?php echo $page->post_content ?>
I tried this as well and I am also not getting carriage returns and I have changed line 9 to ?php echo $page->post_content ?>
Is there anyway of displaying the child page feature image also?
FYI, you are missing the filter before outputting the content.
Try adding the following:
$content = apply_filters( ‘the_content’, $content );
before echoing the content
Hope this helps…
