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.
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 »</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 »</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(); ?>