Advertisement

WordPress Page Templates

WordPress Page templates allow you to create a standalone template (just like page.php or single.php) inside a WordPress theme that the user can selectively use on any specific page she chooses. Although the page-slug.php feature is very helpful, sometimes requiring the theme's user to use the name you choose for a specific feature is too difficult or unnecessary.

As opposed to the page-slug.php feature, a Page template can be used on more than one page. The combined features of user selection and multiple uses make Page templates a much more powerful theme tool than page-slug.php templates.

To make a template a Page template, simply add Template Name: Descriptive Name to a comment section at the top of the template file. For example, this is the beginning of the onecolumn-page.php Page template file in the Twenty Ten theme:

<?php
/**
 * Template Name: One column, no sidebar
 *
 * A custom Page template without sidebar.
 *
 * The "Template Name:" bit above allows this to be selectable
 * from a dropdown menu on the edit page screen.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */ 

This code registers the template file with WordPress as a Page template and adds One Column, No Sidebar to the Template drop-down list found under Page Attributes in the page editor. Using a template on a static page is a two-step process:

  1. Upload the template.

  2. Tell WordPress to assign the template by tweaking the page’s code.

    image0.jpg

The code sample demonstrates how the WordPress developers accomplished creating the One Column, No Sidebar Page template in the Twenty Ten theme; however, in reality, you need only three lines to make it happen. Here is the code that appears at the top of a static-page template:

<?php
/*
Template Name: About Page
*/
?>

The important part is defining the Template Name between the starting and ending PHP function calls. This tells WordPress to recognize this as a unique template and then include it in the list of available templates within the Page Attributes on the Edit Page page (it also appears on the Add New Page page).

By providing a robust set of Page templates, you can offer users of your theme an easy-to-use set of options for formatting different pages within their websites.

blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com