Loading...

Here’s a quick guide on how to generate a list of sub pages in a WordPress template.
Firstly create a page which will act as the ‘Parent’ item and then create some other pages and assign the Parent page to each in the ‘Page Attributes’ box. Then onto our template file in this case i named the page ‘example’ so the template file will be called ‘page-example.php‘. Create the new file substituting you page name for ‘example’ and add in the code below. I have split it into three chunks to explain it, or click here for the full loop script.

<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div<?php post_class( 'example' ); ?> role="main">
<div class="entry">

To begin with I have instigated the main loop with the while statement in order to get the parent page title, post class, content and ID. Using the WordPress functions; post_class(), the_title() and the_content() I output the page information. I then assign the variable thisID as the page ID using
get_the_id() and close off the main loop with the endwhile statement. Note that the post div and entry div are still open as we will place the sub page information in it before we close them.

<?php $args = array( 'post_parent' =>; $thisID, 'post_type' => 'page', 'posts_per_page' => '-1', 'order' => 'asc', ); 

So this little bit sets up the arguments for the <em>WP_Query()</em> function. In this case I’ve passing the page id we got earlier in as the parent page id using the thisID variable which means that only sub pages of this page will be returned. Then I define the post_type as page, do not forget this as WP_Query() defaults to ‘post’ which will return no pages or sub pages. The posts_per_page parameter will return all the sub pages when its set to minus one, or you can place your own integer in there, or alternatively leave it out and the pages will be shown as per the settings in the WordPress dashboard.

Next up we run the new loop.

$query = new WP_Query($args); while( $query->have_posts() ) { 
$query->;the_post(); ?> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">Link to post</a>
<h4 class="sub-page-title"><?php the_title(); ?></h4>
<?php } ?>

</div>
</div>

We begin the new loop by setting up a new instance of WP_Query() using our custom arguments, and setting up the post data if there are any posts with the while statement. Then we created a link to each subpage, using the_permalink(), get_the_title() and the_title(), then we close off the second loop and the surrounding div elements. Voila a list of a pages sub pages each linking to the sub page.

Comment if you have any ideas on how I could do it better!

Here’s the whole thing:

<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div<?php post_class( 'example' ); ?> role="main">
<div class="entry">
<?php $args = array( 'post_parent' =>; $thisID, 'post_type' => 'page', 'posts_per_page' => '-1', 'order' => 'asc', ); 
$query = new WP_Query($args); while( $query->have_posts() ) { 
$query->;the_post(); ?> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">Link to post</a>
<h4 class="sub-page-title"><?php the_title(); ?></h4>
<?php } ?>
</div>
</div>
<?php endwhile; ?>

Latest Posts

Portfolio

Contact

Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.

 

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close