How to Use Page Templates in WordPress

Before I had a custom theme designed by Ryan Burrell, I would find free themes and do my best to modify them into a simulacrum of how I wanted my site to appear. Because I’m not a coder by any stretch of the imagination, this was often a painful process, and the documentation provided by WordPress was often confusing to me. It would tell me the code I needed, but not how to actually do anything with it.

When I began messing with page templates in WordPress, it took a while before I figured out the very simple tricks of how to actually implement them. Templates allow you to impress a great deal of variety on your site, with different pages looking different ways, and I’ll share a few basics with you in regards to setting them up.

Note: You can only use templates on Pages, not on Posts. For the sake of this exercise, we’re going to take a look at an Archive page and how you can modify its appearance and contents.

Name and create your template

There is one very simple component to creating a page template, and that is the meta data located at the top of your new template page. To start off, go into your theme’s files ((For those using a hosted WordPress solution that employs CPanel, that’s probably something like public_html/wp-content/themes/theme_name)) and create a new file titled archives.php.

Note: You have to be careful when naming template files to avoid overwriting existing files in the template. In this case, I already have an archive.php (used to display a list of posts within an archive, such as the page for September, 2009), so I had to name my template something else. ((In addition, some names are reserved by WordPress (such as index or sidebar) so avoid using these names. For a more detailed (and slightly bewildering) explanation, check out Template Hierarchy and Using Themes in the WordPress Codex.))

After you have created this file, open it for editing and place the following at the beginning of it:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */
/*
Template Name: Archives
*/

The key, which I was missing for-freaking-ever, was that “Template Name: <name>” part. It wasn’t mentioned anywhere in the codex (at the time) so even though I had my template all created, I wasn’t able to select it for a page.

With that done, you can build your template. See the attached archives.rtf file for a full example of what my archives template looks like.

Select your template

Once your template file is built and ready, you’ll be able to see it when you create a new page in WordPress. Just select it from the drop down menu on the right!

Select the template using the drop down menu on your new page.
Select the template using the drop down menu on your new page.

Using Different Sidebars

I was asked recently how you would use different sidebars on different pages depending on the template, ((Rather than using a different sidebar for all pages, as my other guide details)) and the process should be pretty straight-forward. ((Though I haven’t tested this myself and, again, not being a coder I’m not confident in my logic here)) Just omit the call for the standard sidebar (using the WordPress get_sidebar tag) and call a different one using the following code:

You would, of course, have to have a sidebar2.php file in your template directory for this to work, but you get the idea.

Using a Second Sidebar for Different WordPress Pages

This post was originally written when I was using a different theme. That doesn’t affect the content, just wanted to let you know that my current theme (designed by Ryan Burrell) doesn’t use this particular method. Rather, it employs various templates to display bars as necessary. For more information, check out my guide How to use page templates in WordPress.

I wrote sometime ago about the visual design of a website, particularly a WordPress blog, and lamented the long sidebar that plagues so many sites. Like anyone else, I want to show everything I have to offer to my readers, but I know that my readers don’t necessarily want the same. End-users want the information they want and nothing else that might distract them from that information. Readers want data to be concisely presented and easy-to-read. A sidebar as long as my arm detracts from these goals.

Therefore, when I added some things to my WordPress sidebar (like a blogroll) that made it particularly long, I knew something had to be done. It’s all fine and well on the main page where ten blog entries make for a rather long site (and therefore the sidebar doesn’t look out of place). But when looking at a single post, the sidebar added a dozen inches below the comment box until you reached the footer. No good.

The WordPress Codex has some tips on creating a second sidebar and using this for other pages (such as when viewing just a single post), but beyond giving me the PHP tag for the sidebar, it didn’t do me a lot of good. The theme I use did not have the sidebar statement in the default location (which is normally sidebar.php in your theme’s files), and when I did find the code I needed to change, the tag listed in the WordPress Codex didn’t work.

My sidebar actually extends up from the footer, so it was the footer.php I needed to edit. Removing the standard sidebar tag, I entered the following:

When I first made this change, I discovered that, while it worked great for posts, my pages still had the longer sidebar. Since my Contact page is pretty short, this ran into the same problem as before: the page content had ended, but the sidebar just kept going. Therefore, the code needed to be modified slightly to accommodate pages as well as single posts.

Using an elseif statement, we are able to use sidebar2.php on both pages and single posts. If you have additional templates on which you wish to use a different sidebar (say you wanted a different sidebar on the category, archive, and portfolio pages as well), you might considering using a switch, but with my limited knowledge of PHP, using if/elseif/else statements worked just fine for me.