Jun15, 2010
Wordpress Child Pages Page... or something
OK, back to srs business posts now...
One of my current top secret projects (it's not that exciting, I'm just helping someone convert their site to WordPress) involves creating pages with a list of child pages on 'em. Back in ye olde days I'd have done this manually (which meant every time a child page was added the parent had to be modified) or farted about with SQL queries but Modern Day WordPress seems to have a function for everything and so it's much simpler. So, without further ado...
Create the template:
<?php
/*
Template Name: Sub Page
*/
?>
<?php get_header(); ?>
<div id="content">
<h2><?php the_title(); ?></h2>
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
?>
<ul>
<?php echo $children; ?>
</ul>
<?php
}
if ($post->post_parent)
echo '<a href="'. get_permalink($post->post_parent) .'">« Back to '. get_the_title($post->post_parent) .'</a>';
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Save it as subpage.php, upload it to your theme directory and then whenever you just want a list of sub (child) pages, select Sub Page from the Template dropdown on the right. Ner nerrr!
Tip: if you wanted a section intro instead of just a list of links, bung <?php if ( have_posts() ) { while ( have_posts() ) { the_post(); ?><?php the_content(); ?><?php } } ?> after the h2 title stuff.
Welcome to the blog of girl geek & php ninja; Jem. Web developer, mum and crazy cat lady talks about parenting, pets and php 



