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 Answer
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' ); ?>