Searching for Children and Grandchildren

I had a request recently for help on using my Query Child Of plugin to create a page template to show the grandchildren of a page as well as a number of people asking for better examples on how to use my plugin in general to just list the children of a page.

I am now happy to announce that I can provide these examples and have updated the plugin documentation on in the WordPress plugin directory to match as well.

Firstly we have an example page template (based on the default theme) for listing the children of a page:


<?php
/*
Template Name: Page with Children
*/
get_header(); ?>

<div id="content" role="main">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div>
<?php the_content('<p>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>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
<div id="children">
<dl>
<?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC'); ?>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a></dt>
<dd style=""><em><?php the_excerpt(); ?></em></dd>
<?php endwhile; endif; ?>
</dl>
</div>
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Next we have an example page template, again based on the default theme, for listing grandchildren:


<?php
/*
Template Name: Page with GrandChildren
*/
get_header(); ?>

<div id="content" role="main">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div>
<?php the_content('<p>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>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
<div id="children">
<dl>
<?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC'); ?>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php     $inner_query = new WP_Query("post_type=page&posts_per_page=-1&child_of={$id}&order=ASC");
while ($inner_query->have_posts()) : $inner_query->the_post(); ?>
<dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a></dt>
<dd style=""><em><?php the_excerpt(); ?></em></dd>
<?php endwhile; endwhile; endif; ?>
</dl>
</div>
</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Hopefully these examples are illuminating!

2 thoughts on “Searching for Children and Grandchildren

  1. Hey westi, Looks like & has been mangled in the code… I have a feeling that “post_type=page&posts_per_page=-” isnt going to work as expected..

Comments are closed.