0

On a parent page I want to display all the childpages (first child only) including its template.

For Example

Pages:

Parentpage (Templatename: parentpage)
- Child A (Templatename: childpage)
- Child B (Templatename: childpage)
-- SecondChild B (Templatename: secondchildpage)
- Child C (Templatename: childpage)

In the Template parentpage I have this:

 <?php
    getchilds = array(
        'parent'        => $post->ID,
        'child_of'      => $post->ID, 
        'sort_column'   => 'menu_order',
        'sort_order'    => 'ASC'        
    ); 

    $postliset = get_pages($getchilds);
        foreach ($postliste as $post) {
        echo $post->post_content;
    }
?>

This displays the content of all the childpages on the parentpage. So far so good, but I want it to include the templatepart of the childpages as well. So if my childpage template would say:

<div class="childpagestest">
[...]
        <?php the_content(); ?> 
[...]
</div>

The parentpage should include this code.

I searched the whole function codes of WP for something like the_template but nothing.

Any ideas?

Thank you very much in advance.

0

I would try setting up post data in the loop and then calling a page template using get_template_part function. Note: depending on theme it may produce some unexpected results, as page template sometimes also includes site header and footer. But it's something to get you started.

$getchilds = array(
    'parent'        => $post->ID,
    'child_of'      => $post->ID,
    'sort_column'   => 'menu_order',
    'sort_order'    => 'ASC'
);

$postlist = get_pages($getchilds);

foreach ($postlist as $post) {

    // setup post data, so page template will use it as a "master" post
    setup_postdata($post); 

    // we get page template name for the post and remove ".php" at the end to make it work
    $template = preg_replace("/\.php$/", "", get_page_template_slug($post) ); 

    // now let WordPress fetch that page for you
    echo get_template_part($template);        
}
  • Thanks, that was what I was going for. I will strip the get_header() and get_footer() and carry on. Only thing is, I can't strip the get_content() and surely it will display the content of the parent and not the child :( I will have to find a workaround for that. Thank you very much! – LexLusa Jul 20 '17 at 22:55
  • setup_postdata($post); in theory is supposed to "switch" context of the current post, so inside your page template, you supposed to see content of the child page... If this doesn't happen, try maybe adding global $post; line somewhere before foreach. It worked on my test site, but there could be some variation theme to theme... Oh, and stripping header and footer can be easy - just define a variable in parent template, then check it inside child page (and skip header/footer includes) – Worduoso Jul 25 '17 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.