Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
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.