jump to navigation

Try Firefox - it's faster, and more secure than internet explorer. It even has a cute little mascot.

Journal

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.

Comments »

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

  2. 2. Andrew Nowak
    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 :)

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

  4. 4. Ben
    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?)

  5. 5. Libri e film
    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.

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

  7. 7. John Doe
    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 ?

  8. 8. John Doe
    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.

  9. 9. aytac
    July 16, 2007

    thanks very nice…

  10. People who link to this post...

Leave a message tell me what you're thinking

Please feel free to comment - I like receiving them - however I would appreciate you using your real name. I am not here to improve your search engine ranking. Abusive/ Inappropriate posts and advertisements will be deleted.

Binary Moon