Ben
Ben is a lifelong Nintendo fan who likes to build websites, and make video games. He buys way too much Lego.
WordPress and Games
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.
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 🙂
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.
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?)
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.
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.
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 ?
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.
thanks very nice…
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
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.
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.uk/tools/w3c/ .
Cheers,
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.
Thanks! 😀
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)
Great tip! Thanks alot!
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?
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”);
Short and to the point. Thank you! Any suggestions on how to set up different categories into different pages using templates?
I have done all as mentioned but still my archives page http://www.sfihomebizz.com/archives/ is not showing any content. http://codex.wordpress.org/Creating_an_Archive_Index mentions adding custom field _wp_page_template. But this is not shown in my post or page custom field lists. How to solve this?
What a shit article.
You are missing quite a few technical setails here sir.
What a shit comment. You are missing quite a few constructive details here sir.
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!
I don’t see how a plugin could be used for this. It requires scripting.
And how to make custom template/design for some category?
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