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.

WordPress 3.0 and Menu Management

Everyday more websites use WordPress in ways that stretch beyond the standard “blog.” Shared hosts offer inexpensive LAMP hosting and its friendly interface makes basic content editing a snap that is until the editor needs to change the navigation.  To close this gap, version 3.0 introduces a new menu management system that brings plenty of control and flexibility.  Although with the added flexibility comes new complexity while also side-stepping a fundamental issue with using WordPress to manage 50+ pages — changing a page’s position within a site’s structure is painful.

What’s New?

Tucked under the Appearance, a new menu Appearance > Menus has been added that provides users an interface for creating and editing menus. A menu may consist of any number of pages, categories, or custom links arranged in any order and structured hierarchically. Objects are positioned using the same drag-and-drop interface found elsewhere in the Admin UI.

Once created the menu needs to be placed into action.   There are two options: (1) add the “Navigation Menu” widget to a widget area, or (2) add the template tag wp_nav_menu() to your theme.

Site Structure vs. Navigation Menus

At Boston University, our WordPress MU CMS is a replacement for websites that used to be maintained using DreamWeaver/Contribute.  A typical website such as the International Programs website, which launched a couple of months ago, consists of 20-150 pages organized in a hierarchical structure.   (The “Find Programs” Section of this website breaks from this model by using taxonomies to provide multiple pathways for discovery.)  Once a visitor clicks on a top-level page, the navigation widget in the sidebar adapts showing the children of the current top-level page, which are the most relevant pages.

Navigation Management Plugin Screenshot
Screenshot of BU’s custom Navigation Management plugin.

Instead of the user being limited to typing a number in box (menu_order) and selecting a page parent from a never-ending drop-down, BU invested substantial time and energy in a custom tool for managing a site’s structure.   In addition, to making it easy to position a page, our tools add custom navigation labels, provide users with the ability to exclude pages from the navigation, adds support for custom links, and improves performance substantially.   The system works great for the main navigation, but there are a few issues:

  1. Moving pages changes the permalink without creating 301 redirects.
  2. Creating custom utility, side, and footer menus is not possible.
  3. Creating microsites that do not share any navigation with the main website is not possible.

Criticisms of the Menu Management Approach in 3.0

WordPress 3.0 does nothing to improve the management of a site’s structure.  The new menu management functionality is completely separated.   Changing a page’s parent or adding a category does not modify existing menus, so every change has to be replicated in the menus.   Larger sites with lots of pages will still need to depend on wp_list_pages() or some other custom solution to achieve the common behavior of listing child pages or child and sibling pages of the current page.

The good news is that this hole could be addressed in the future.   Perhaps a combination of the BU Navigation Management tool used to manage a site’s structure coupled with an improved version of the menu management tool being released in WordPress 3.0 would achieve the best of both worlds.

I wonder what the rest of the community thinks…