I am trying to make a template that will be an html5/ vertical scrolling base.

I have created an amount of page templates that each would represent a <section> on index.php which mainly all it does is to retrieve the child pages of a page I named ROOT using this:

// Set up the objects needed 
$my_wp_query = new WP_Query(); 
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

// Get the page as an Object 
$mainpage =  get_page_by_title('ROOT');

// Filter through all pages and find Portfolio's children 
$children = get_page_children( $mainpage->ID, $all_wp_pages );

Is there a way to render the output with the each page template? The page hierarchy is like this:

- Page1 (template: foo1)
- Page2 (template: foo2)
- Page3 (template: foo3)
- Page4 (template: foo1)

so what I am trying to achieve is to have an output like this:

<section class='page<?php the_ID(); ?>'>
[content with template foo1]
<section class='page<?php the_ID(); ?>'>
[content with template foo2]
<section class='page<?php the_ID(); ?>'>
[content with template foo3]
<section class='page<?php the_ID(); ?>'>
[content with template foo1]

1 Answer 1


The snippet from codex is a bit redundant in your case. You can achieve what you need, by using snippets like these:


$mainpage = get_page_by_title( 'ROOT' );
$the_query = new WP_Query( array(
    'post_parent' => $mainpage->ID,
    'post_type'   => 'page',
) );

$index = 0;
while ( $the_query->have_posts() ) :
    echo '<section class="page' . get_the_ID() . '">';
    get_template_part( 'foo', $index++ % 3 + 1 );
    echo '</section>';



<div id="template-foo-1">
    <?php the_content() ?>


<div id="template-foo-2">
    <?php the_content() ?>


<div id="template-foo-3">
    <?php the_content() ?>
  • Not a bad approach, but I went with creating shortcodes that would deliver the html necessary for each section. Nevertheless, it is helpful for another project I have in mind.
    – Panagiotis
    Feb 20, 2013 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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