Using Page Templates to be More Expressive

Everyone has heard the term “web page.” To even a novice WordPress user, creating and editing web pages just makes sense. For developers of custom themes, pages have expressive capabilities that exceed their cousin the standard WordPress post.   Pages are organized in a hierarchy, and users control which template is used to render the page.

Page templates are powerful.  They give developers control over how the content is rendered, what content appears on the page, and when applied to multiple pages function as a basic label.

Creating an Author Profile Template

Let’s say that we are running a blog with multiple authors contributing content.   Within the “About” section, we want to create profile pages for each author that include a dynamic listing of the author’s most recent posts.  To start, duplicate the normal page.php template and rename the file author-profile.php. For the template to show in the WordPress list of page templates, we need to name the template.

<?php
/*
Template Name: Author Profile
*/
?>

The next step is to create a secondary query that lists the five most recent posts by the author. For more information on the parameters that control how the query is initialized see the WordPress Codex.

<?php $author = get_post_meta($post->ID, 'author_profile_name', true); ?>

<?php if (!empty($author)) : ?>
	<?php
		$args = array(
			'post_type' => 'post',
			'post_status' => 'publish',
			'posts_per_page' => 5,
			'author_name' => $author,
			'paged' => false
		);

		$author_query = new WP_Query($args);
	?>

	<?php if( $author_query->have_posts() ) : ?>
		<ul>
		<?php while( $author_query->have_posts() ) : $author_query->the_post(); ?>
		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php endwhile; ?>
		</ul>
	<?php endif; ?>
<?php endif; ?>

For this to work properly, a custom key named “author_profile_name” must be created and the author name supplied as the value, which is not exactly user-friendly. Since the page template functions also as a label, we can use this information to create a meta box that is only loaded when the “Author Profile” page template is applied. The meta box includes a simple drop-down that includes all blog authors.

Note: This is just a barebones example that should be secured using a nonce and permissions check.

function gc_author_meta_box_profile($post) {
	$current_author = get_post_meta($post->ID, '_author_profile_id', true);
	echo '<label for="author_profile_id">Profiled Author</label> ';
	$args = array('id' => 'author_profile_id', 'name' => 'author_profile_id', 'show_option_none' => '-- Select --');
	if(!empty($current_author)) $args['selected'] = $current_author;
	wp_dropdown_users($args);
}

function gc_author_meta_box_save($post_id, $post) {
	 if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
	 if($post->post_type == 'revision') return;

	 update_post_meta($post_id, '_author_profile_id', strip_tags(trim($_POST['author_profile_id'])));
}

add_action('save_post', 'gc_author_meta_box_save', 1, 2);

With the inclusion of  custom post types in WordPress 3.0, the number of possible uses for pages as containers for other content only increases. For example, a real estate site could have pages for different neighborhoods that include listings found in the neighborhood. The only stumbling block is that you will need to use a plugin if you want to easily manage the site’s structure.

Leave a Reply

Your email address will not be published.