How to Create Page Templates In WordPress
Custom page templates in WordPress can seem daunting at first, but once you understand the concept behind them and how to apply it, they're pretty straightforward. Here's how they work…
As you probably know, WordPress keeps all your content in a database, and it uses themes to determine how to display that content. That's why when you change your WordPress theme you can get an entirely different looking blog with exactly the same content – it's because content and presentation are kept separate.
Each WordPress theme is kept in it's own folder which contains specific files for displaying the various pages of your blog. For example, if there is a file called "single.php" in your theme folder it will be used to display any single post on your blog. By changing "single.php" you can alter how single posts are displayed.
Because we're going to be creating a custom page template, we'll be looking for a file called "page.php" in your theme folder. This is the file which determines how individual pages are displayed. The first thing we want to do is make a copy of it and name it something like "template_page.php" so we can identify it later. By the way, if your theme doesn't have a file called "page.php" you can just make a copy of the "index.php" file instead.
To make this page into a template which WordPress recognizes (and that you'll be able to use), we need to add the following code right at the top of the file;
<?php /* Template Name: My Template */ ?>
The text after "Template Name" can be anything you like, but bear in mind it will appear in the "Page Template" drop down box, so be sure to give your template a meaningful name. Once you've copied this file to your theme folder it will be available to WordPress.
Well, believe it or not, you have just created a page template. Quick, let's see if it works… Go to your blog admin section and create a new page, but before you publish it, scroll down and look for a "Page Template" section with a drop-down box. Click it and you should see your template listed in there – it's name will be the same text you put after the "Template Name" at the top of the file. Select your template name and publish the page. Congratulations!
Hmmm… it probably doesn't look any different. Well, that's because we didn't actually change anything in the file, apart from making it into a template, so it will display content in exactly the same way as the file we copied. Let's make some changes and see what happens…
In your new template file, you've probably got a line of code that looks something like this;
<h2><?php the_title(); ?></h2>
That basically tells WordPress to display the Title of the page. Add an extra line right after it that contains this;
<?php echo '<p>This page was produced by my excellent page template!</p>'; ?>
Browse to the page which uses your page template, and you should see the text "This page was produced by my excellent page template!" right after your title.
That's pretty much how page templates in WordPress work. I find them perfect for such things as contact pages and sitemaps ..etc, but you can put just about anything you like in your template and it will be displayed by WordPress. Your imagination really is the limit.
As an example I've created a page template which aims to help you find a ClickBank winner by showing items from the ClickBank marketplace that have a minimum commission and gravity. I used exactly the same principle as I've outlined in this post. Let me know how you get on…
del.icio.us
Digg
Propeller
StumbleUpon
Reddit
Furl

Hi there. My name is Peter Coughlin. I am a freelance web developer living in the UK, and at the moment I am specialising in WordPress customisation.
2 Responses so far ↓
Nov 1, 2009 at 9:48 am
Would be possible to use this strategy to create a single post page? How can I input code for a specific post w/in a new page template?
Basically, I want to generate a home page that displays 1 post, but has all the characteristics of a single post page, i.e., design, designation, etc.
Nov 12, 2009 at 4:45 am
Hi Scott. Hmm.. there are easier ways of having just 1 post on your home page. Have a look at static front pages and sticky posts on wordpress.org for some ideas.
Page templates are used by pages rather than posts. You can have pages that display posts, but that’s beyond the scope of this post :-)
Tell me what you think...
Note: your comments may need to be approved before they are shown.