A parent template page to display child page list and details

During this WordPress theme development (based on thematic), I was looking for how to create a nice parent page. Basically in the navigation menu there is a page “Projects” and each project is a subpage. But you have to create the “Projects” page content to describe what is this part (you also can redirect to the first child page, but I dont like it).

Anyway, this function displays each child page title, link and a description. The description is stored in a custom field “desc” in each subpage.

About

This function is actually easy but could help you to save some time.

The function is based on get_pages(). To make it works you need to create a page and some child page. In each child page create a custom field with the key “desc” and fill the value with a little description.

Install

Add this function is your functions.php.

<?php
 
/*
 *   Template Parent Page
 **************************************************/
 
function sigerr_display_subpage_list($parentID)
{
	//Get all the child page of the current page
	$pages = get_pages('child_of='.$parentID.'&sort_column=post_date&&sort_order=asc&echo=0');
	?>
 
    <div id="childpagelist" class="">
 
	<?php
	//Echo the list
	foreach($pages as $page) {
		$link = get_permalink($page->ID);
		?>
        <h2>
        	<a href="<?php echo $link ?>" title="See the entire page about <?php echo $page->post_title; ?>">
				<?php echo $page->post_title; ?>
            </a>
        </h2>
        <p>
        	<?php if( get_post_meta($page->ID, "desc", true) ){
				echo get_post_meta($page->ID, "desc", true);
			}else{
				echo "No details available, let's check that page!";
			}?>
 
			<p>See: <a href="<?php echo $link ?>"><?php echo $link ?></a></p>
 
        </p>
        <?php
	}
	?>
 
    </div>
 
	<?php
}
 
?>

This function is now available in your theme

Create a Template Page

Create a new template for wordpress page. If you dont know how, just copy your page.php file and rename it in ‘template-page-parent.php’.

Then add this at the very top of the file.

<?php
/**
 * Template Name: Parent
 *
 * This template display a list of each child page with details.
 * Details are stored in a custon field 'desc' in child pages
 *
 */
?>

Then locate the piece of code “the_content()”. This function print the content of the post. You should probably print the list just after this function.

<?php display_subpage_list($post->ID) ?>

This function print the list into a div#childpagelist.

If you get any trouble, feel free to leave a comment. :)

I rewrote this post as a project.

Related Posts:

No related posts.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">