Using Query_Posts to list Child Pages

7:38:00 AM |


WordPress’ function query_posts(); is great for grabbing the exact data you need to display whether it be a specific WordPress category, tag, post or page. You can use it for in a number of clever and creative ways. Let’s say you have a top-level page and want to display the data of all subpages below that parent page. Here’s a WordPress loop that utilizes query_posts to do grab all subpages, or child pages, of a top level parent page.
List Subpages using Query Posts

The query_post code and loop

	<div id="content" >
  <?php query_posts('post_type=page&post_parent='.$parent); while (have_posts()) : the_post (); ?>
	 <?php the_ID(); ?>
	 <?php $parent = $post->ID; ?>
	 <div id="post-<?php the_ID(); ?>" class="post" >
		<h1><?php the_title(); ?></h1>
 
				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
 
				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
	</div>
		<?php endwhile; ?>
	</div>

A Page Template with Loop

<?php
/*
Template Name: List Subpages
*/
get_header(); ?>
 
	<div id="content" >
        <?php query_posts('post_type=page&post_parent='.$parent); while (have_posts()) : the_post (); ?>
 
         <?php the_ID(); ?>
	 <?php $parent = $post->ID; ?>	
	 <div id="post-<?php the_ID(); ?>" class="post" >
		<h1><?php the_title(); ?></h1>
 
				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
 
				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
	</div>
		<?php endwhile; ?>
	</div>
 
<?php get_sidebar(); ?>
 
<?php get_footer(); ?>