Subpages directory in WordPress

When you have a set of pages that are subpages of a main page (e.g. the case of this website for a complementary health clinic) you may want an automated system that creates a list (or directory) of subpages and avoid double editing when you add or modify a subpage. We used this to create a practitioners’ page that list all practioners in the clinic.

There is a simple way of defining a new template that, based on the standard page template, adds the list of subpages under the page content.  We use the WP_Query() function that accepts hundreds of combination of different parameter and allows to select any combination of posts, pages and categories in any order you can possibly think about.  Here is a sample of the code to be added immediately after the page loop code:

<section id="practitioner-container">
   <?php
   $args = array(
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'page',
          'post_status' => 'publish',
          'post_parent' => '479',  // id of main page
          'posts_per_page' => '-1' ); // all posts 

   $subpages = new WP_Query($args);
   if($subpages->have_posts()) :
      while($subpages->have_posts()) : $subpages->the_post(); ?>
      <div>
        <div>
        <a href="<?php the_permalink() ?>">
        <?php the_post_thumbnail(array(60,60)); ?></a>
        <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
        <?php echo substr($post->post_title,0,70); ?></a></h3>
        <?php echo "<p>"; the_excerpt(); echo "<br />";
        echo twentyeleven_continue_reading_link() . "</p>"; ?>
       </div>
      </div> <?php 
      endwhile; 
   endif; ?>
</section>

 

Leave a Reply

Your email address will not be published. Required fields are marked *