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.

3 thoughts on “How to Use Page Templates in WordPress

  1. Heya, Matthew!

    Thanks VERY, VERY, VERY much for this! I recently worked out how to make a page template (I agree with your other tutorial in which you say it’s hard to find in the codex!) and then I was faced with this problem of having what appears to be a stupidly long sidebar on pages where there is only a short list of links or the like.

    I hadn’t thought of a simple php-include! Ha ha! Very nice.

    I actually had problems with forgetting to close the loop (I know next to nothing about coding, incidentally) so for anyone who wants to try this trick of making a different sidebar and then making a Page template and calling the second sidebar in that template, here’s my advice:

    * go to your Theme Editor and copy your sidebar.php file into a text editor.

    Then cut out everything except the following:

    [[[div id=”right”
    [[[?php if ( !function_exists(‘dynamic_sidebar’)
    || !dynamic_sidebar() ) : ?]]]

    YOUR NEW SIDEBAR CONTENT GOES HERE

    [[[?php endif; ?]]]
    [[[/div]]]

    ***NOTE: I’ve replaced the regular angled brackets with [[[ and ]]] here because I don’t what will happen if I just put code directly into this comment form.

    Your sidebar may be slightly different, of course. I don’t know. But I think it should have at least these basics. (**shrug** I really don’t know how theme designers work and if this is a standard format)

    Anyway, the reason I’m posting is that I’d cut out the final “endif” statement inadvertenly and was getting a parse error when I tried to load the demo page.

    It’s all working nicely now and I can drop basically whatever HTML and php I like into where it says “YOUR NEW SIDEBAR CONTENT GOES HERE”

    Hope that helps with this tutorial!

    All the best,
    TheNightOwl

  2. Just used your walk trough to create several page templates and sidebar templates – this way you can specify a unique sidebar to each page! Excellent tutorial – very appreciated – thank very much!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s