0

I am tring to write a loop to get the content of a current-page; and below the content of every children of this page. After reading on wordpress codex website and outside on the web; I can't still figure out the solution....

1
  • Please post what you have tried and be specific about what parts are failing / not achieving what you are trying to do. Commented Jul 18, 2013 at 15:25

1 Answer 1

0

Super basic example that gets the parents title and content and then the title and content for each child page. This is not just the loop, but an entire page template.

   <?php include_once( 'header.php' ); ?>
    <?php
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="post-content">
    <?php echo '<h2>'.$post->post_title.'</h2>'; ?>
    <?php the_content(); ?>
    </div><!-- post content -->
    <?php
    $children = get_page_children( $post->ID, $all_wp_pages );
    if($children != null){
    foreach($children as $child){ ?>
    <h2><?php echo $child->post_title; ?></h2>
    <div class="post-content">
    <?php echo $child->post_content; ?>
    </div>
    <?php
      }
    }
    ?>
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    <?php include_once( 'footer.php' ); ?>

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.