Articles & Columns » Words on WordPress

Below is one of my Words on WordPress articles. My archives are here and you can subscribe to my RSS feed with your feed reader or by by email.

Words on WordPress: Making and Using Page Templates

If you do much with WordPress, you will discover the need to create page templates at some point. Page templates are theme files that produce the layouts for pages, posts, custom post types, category pages, and more. In this context, “page” means more that just a page: it means a file template that helps to show any content in a theme.

When do you need page templates? All the time, really. Any WordPress theme will have page templates included in it: for the index page, for a single post, for a single page, etc. If you are happy with the way a theme displays content, you won’t need any new templates and can do all the changes you need in the style sheet in the theme, i.e. style.css file in the theme folder. (Remember to make a Child Theme Child Theme rather than edit the original theme itself.)

But you may decide you want have a page that doesn’t show the time and date, or one that doesn’t show the author’s name, or maybe a page without a sidebar. This is where page templates come in; you can make a page template with or without features and html, and select that template in the page editor to be used by that page.

The documents are helpful to see how Page Templates work in terms of where they go in a theme folder, the hierarchy of how they are processed, and much more. But you may not need to know all those details. If you just need a page template for a specific thing, templates are easy to make and use.

First, you need to look at page templates as they exist in your current theme. Themes from the theme directory must follow some standards for theme architecture and PHP code, while page templates for frameworks such as Genesis can be very different in format and code.

All page templates will show something like this in the header of the file, which is a block of commented text along with the name of the template:

 * 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
get_header(); ?>

The text that is in between the /** and the */ are comments and notes for users like you and me and the notes are not processed as programming or PHP code. The get_header(); ?> is PHP code and is processed. The “Template Name: One column, no sidebar” is what appears in the Page Attributes meta box in your page editor.

Page Attributes Meta Box as seen in the editor

If you had a page template for a page you wanted called My Custom Page, then you’d copy the page template file above and name the new file something like page-my-custom-template.php. And change the name in the file header to “My Custom Page”, and this would appear in Page Attributes meta box when you select that template for use by a page, as in this image on the right:

And now, of course, you need to change the code in your new page template to match what you want it to do.

And, don’t forget the cardinal rule of editing themes: if you need to add a page template or otherwise edit a theme, make a child theme. See my other Words on WordPress post on Creating a Child Theme


StudioPress Theme of the Month

Related articles:

Blue says you should check out these other Words on WordPress articles: