WordPress get_pages() within get_pages() loop?

Viewed 3863

My theme is doing a one-page design and i have to output subpages in a nested way. So i thought i simply can use get_pages() two times like that:

<?php 
    $posts = get_pages(array(
        'sort_column' => 'menu_order',
        'parent' => 0,
    ));
    $i = 0; foreach($posts as $post): setup_postdata($post);
?>

<div class="center">

    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>

    <?php
        $children = get_pages(array(
            'sort_column' => 'menu_order',
            'parent' => $post->ID,
        ));
        foreach($children as $child): setup_postdata($child);
    ?>
        <div class="slide">
            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
        </div>
    <?php endforeach; ?>

</div>

<?php $i++; endforeach; ?>

It doesn't seem to be that easy though. The second the_title() doesn't work properly. It always outputs the title of the parent page.

How has something like this to be done correctly?

3 Answers
Related