WordPress tips and tricks – Custom Page Templates

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 want. 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 custom WordPress 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) in your theme directory and add a small PHP comment to the top of the page. 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 change the template file to do anything you like. Below is a very simple example of an archives page template.

<?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 custom 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 features. In Elemental I have added over a dozen different custom page templates. 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.

Posted in: Web Design

32 thoughts on “WordPress tips and tricks – Custom Page Templates Leave a comment

  1. 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.

  2. 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 🙂

  3. 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?)

  4. 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.

  5. 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.

  6. 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 ?

  7. 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.

  8. Pingback: Including A Better Navigation For WordPress Pages
  9. Pingback: It's a plugin mashup
  10. Pingback: BinaryMoon Links Competition
  11. Pingback: How to Set Up Custom Wordpress Category Templates in Four Easy Steps
  12. Pingback: links for 2007-10-10 « Ronaldo Folgazão Richieri
  13. 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

  14. 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.

  15. 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.

  16. 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/archives/
    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)

  17. 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?

    1. 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”);

  18. Pingback: Custom Page Templates - Calling An External CSS File | More Than Scratch The Surface
  19. Pingback: Art Direction › Binary Moon
  20. Short and to the point. Thank you! Any suggestions on how to set up different categories into different pages using templates?

  21. Can anyone recommend a WP custom page templates plugin?? I’m looking for one.. I really want to learn everything about coding but I just don’t have the time.. so I think a plugin can help out for now.. 🙂 thanks!

  22. Hi,
    Actually I am very new about wordpress. My question is ;?f I download any wordpress templates or themes how can I customize and what program I need it like a control panel. I guess sound bit silly but I do not have any clue for that.
    Thank you guys advance,
    Ray

Leave a Reply

Your email address will not be published. Required fields are marked *