Justin Tadlock

Page templates: The untapped potential of WordPress

I frequently get questions about how to tweak something on a blog’s front page or change how something works in a theme. Many times, users don’t understand how easy this could be. I see this as a major problem because so many people are missing out on one of WordPress’ greatest features.

Page templates.

They give you the ability to do what you want without touching other theme files. You can make page templates look and function completely different from the rest of your site.

Last year, I ran a series on my blog called If you were a WordPress theme developer. This series was meant to give end users a chance to voice their opinions on how they’d develop a theme if given the chance. The first post dealt with page templates. I had a good reason for this. I wanted to take some of those ideas and apply them to my Hybrid theme.

At the time, I had searched endlessly on Google for page template ideas. There were hardly any that I could find. I saw many tutorials on how to create a basic page template, but that was pretty much it. That brings us to the here and now. I’m starting a new series on creating page templates.

I’ll be giving you the basics in this post, but I’ll follow it up with several tutorials on actually creating something.

What are page templates?

They’re just like any other template within the template hierarchy — they display your content. But, page templates are kind of special. They’re only used when you choose to use them. And, they tend to function a little differently from other templates in your theme.

Most themes should have a page.php file. This is called the Default page template. It usually just loads your page content and displays it like a single post. Themes can also have additional page templates that display things in a different way, add new functionality, and/or do pretty much anything you want them to.

Two good examples of page templates are in the WordPress Default theme — Archives and Links.

I do want to point out that page templates are not for styling specific pages. A good theme will give you CSS classes to style your pages with. Page templates should be about making something function differently from the norm.

How to create a page template

For the first post of this series, I’ll show you how to create the most basic page template. What you need to do is copy your theme’s page.php file. If your theme doesn’t have this file, trash it. It’s worthless.

At the top of this file, you’ll likely see something like this:

<?php get_header(); ?>

That’s pretty standard. We want to change that a bit. First, we need to come up with a unique name. For the sake of this example, we’ll call this template Example. Change the original line of code to this:

<?php
/*
Template Name: Example
*/

get_header(); ?>

Save it as example.php. That’s it. You’ve now created a page template. It won’t do anything special just yet, but that’s okay.

You can download my example template to have a look at one way this can be done:

How to select a page template

Page templates don’t just magically start working once you’ve created them. You actually have to select them when writing a page. Hop on over to your WordPress admin and create a new page. Find the meta section labeled Attributes. Within that section, select your template from the Template drop-down box.

The Example template we created in the previous step may be your only page template. As you can see in the figure below, I have quite a few to choose from.

Select a page template
Figure 1: Select a page template

Once you’ve selected a template, save your page and view it on your site. The Example template won’t do anything different from the default page.php template, but I’ll get to that in the next tutorial.

Gearing up for the page template tutorial series

It’s idea time. Put on your thinking caps and let me hear some great ideas. Just don’t get too crazy. I probably won’t do anything like add JavaScript, hook into Twitter, or anything complicated. I just want to show how we can grab information from WordPress and use it in unique ways.

I’m fascinated by all of the things that can be accomplished with WordPress page templates. From my experience, few users take advantage of this feature of WordPress. And, those that do might only use one or two templates.

Like this tutorial? Please consider helping me write more in the future by making a donation via PayPal, grabbing something from my Amazon Wish List, or signing up at Theme Hybrid where you can ask me any support questions you want.