WordPress tips and tricks - custom templates
June 30, 2007 | Web Design
Some time ago I posted the first of my tips and tricks for WordPress, and I thought it was about time I posted some more so, to start things off, here is a short tutorial on custom page templates in WordPress.
Custom page templates are very much an underused part of the WordPress software yet they can do some very powerful things. Basically you can create a new template file and then use that to display WordPress pages in a totally different way to normal, adding whatever extra features you desire. An example of a custom page on Binary Moon would be the Archives page (I need to rejig my archives, they are getting rather long).
Creating a page template is very easy indeed - all you need to do is create a new template file (I tend to copy an existing page template) and add a tiny amount of code at the top of the page. The file should be uploaded to your theme folder. Once this is done the template will show up in the “page template” section of the write/ edit page screen.
The code should look like…
<?php /* Template Name: Name of template here
*/ ?>
Make sure you replace “Name of template here”, with the whatever you wish to call your template.
To use the template simply go to the write page screen, find the “Page Template” panel, and select the template you want to use. Hit save and the page will be formatted using the new template.
Once this is done you can modify the template file to do whatever it is you want it to do. Below is an example of an archives page.
<?php /* Template Name: Archives */ ?> <?php get_header(); ?> <h1>Archives</h1> <h2>Archives by Subject</h2> <ul> <?php wp_list_cats(); ?> </ul> <h2>Archives by Month</h2> <?php wp_get_archives(); ?> <?php get_footer(); ?>
Archives aren’t the only thing the page templates are good for - you can use them for many different things - for example you could make a custom contact page with a contact form, or a new style of links page using the built in blogroll feature. There are many possibilities. I’m interested to see what others have done so if you have an unusual use for a custom template please post about it in the comments below.
Article Series
this post is part of a series of articles - why don't you check out the others below?

Comments »
July 2, 2007
Thanks for the tips and very interesting article. btw. I really enjoyed reading all of your articles. It’s interesting to read ideas, and observations from someone else’s point of view… makes you think more.
July 2, 2007
I think that this article isn’t written for less experienced users. For me it’s not looks very clear -Probably I should stay with Blogger
July 2, 2007
In regards to your archive page getting long, why not check out my clean archives plugin that solves that issue?
You can find it on my sites project page. I didn’t want to directly link to it in a comment. Not sure if that was ok or not.
July 2, 2007
Sean - I’m using an older version of your clean archives plugin. I prefer it without the show/ hide javascript stuff, although I agree that it would be a solution to the page length. I actually want to rejig the layout anyway but intend to keep a page with a complete article list so maybe I should install it for that page. I have no problem with direct links either… provided they add value to the conversation. I’ve edited your comment to add the link in
Andrew - you’re right. You need at least a passing knowledge of php and wordpress theme development, although if you have a basic knowledge it’s not too hard to do (I hope?)
July 4, 2007
This article is very intersting. I’m trying to do something a bit different, but for the moment I’m not able to do that.
I would like to make it possible to have a single post at the top position in some categories of my blog. The problem is that article has to be a different one in each category.
July 6, 2007
I’d like to have a page template that shows only one side bar for posting images/ image gallery.
I currently use “garland” theme (hacked for WP usage) by Chris Meller. I understand the concept of renaming/modifying the existing page.php template to a new template, and appreciate your hints in this post… however… it seems that in the theme I’m using, the call for sidebars (left and right with content in middle ie. 3 column look) that the “code” that defines the l-r sidebar-columns, is written within the style.css !!
So, do you know or suggest how I can hack this so that the change would only apply when using the new 2 column page template?
Any help on this matter appreciated. Thx.
July 7, 2007
surely a good read!!
One of the thing I do with page templates, besides archives is my site map. I however don’t know how NOT to display the site map link in the top navigation bar. Though I am distracting you from the template topic, I guess the answer lies in meta_key / meta values when you display page links using wp_list_pages ?
July 7, 2007
I got it. It can be done using custom fields as I thoght, but these custom fields did not work the most intutive way. You can see this in action on my site. I will post an article on this soon.
July 16, 2007
thanks very nice…
July 10, 2008
Hi,
I have to admit I found this post very useful, I was struggling alot with adding new page templates, and I found everywhere else was really complicated, and worded it so I couldn’t understand, but you did it nice and simple, Thanks, now bookmarked your website
Craig Farrall
August 25, 2008
Hi,
I am facing screen resolution problem with Garland template for my site, not all would be able to view it properly and my site get messed up. I would indeed like to have horizontal scroll bar for my screen to get it fit. My site is http://innovideas.blogspot.com
can you let me know solution for this.
August 26, 2008
Hi,
Nice post, I’ve been using Custom pages for some time. I’ve created a page where the user submits there Google XML Sitemap, it then goes off and checks each page against the W3C Site. This can be seen here: http://www.anthonyshapley.co.u... .
Cheers,
August 27, 2008
Abishek - your site is not made in wordpress. I can’t help as I’ve never used blogger.
Anthony - nice! I hadn’t considered using custom pages for anything beyond different designs, but additional functionality is a good idea too.
September 5, 2008
Thanks!
November 23, 2008
Ben - I really learned a lot from this little tutorial! Thank you so much.
I spent an hour or so, learning about the template tags used to generate the lists within archives.php.
However, I couldn’t figure out how to break my Monthly list into date - post title like you’ve done on http://www.binarymoon.co.uk/ar...
Can you email me the php code to get the Monthly list to break down like that?
(BTW, I’m working on a local install of mimbo_pro at the moment)
March 23, 2009
Great tip! Thanks alot!
March 23, 2009
One question… What about if I wanna use a diffrent header in the new cus template. In my header I list the categories. But in one page I don’t want it there. Any tips?
March 23, 2009
You can modify whatever you like in the custom templates. There’s no rule that you have to use the get_header() call. If it’s a big change then there’s no reason you couldn’t use a totally custom header file eg include(”header2.php”);
People who link to this post...