WordPress template files for pages, part 2
My previous post, WordPress template files for pages, part 1, was all about why I use template files for most of my pages. The main reason is formatting, but of course there are other benefits, such as being able to use php without needing a plugin.
That now brings me to the second part, where I will explain how to set up a template file and how to use it. Using any good text editor (I’m now using Notepad++ for developing purposes), create a new file. We need to give it a name. To do this, we need a little bit of php:
<?php
/*
Template Name: Template name goes here, such as Credits, Links, Downloads - whatever
*/
?>
This gives it a name for when you need to access it and create your page. Believe me, it makes life easier. The next step is something that is carried over in all WordPress template files (9 times out of 10 anyway!). We want to call the header:
<?php get_header(); ?>
Again, the next step is just as simple and is similar to creating a page, single article or index page. This is where we start the formatting with (X)HTML, so we can make it display like any other page on your blog:
<div id=”primary”>
<div class=”entrytext”>
Everything goes in here.
</div>
</div>
Finish off your template file with the usual as well:
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
And there you have it. A simple and straight forward template file that you can use to create any kind of page you like. You simply need to save it with the .php extension. Download mine.
So, how do you use these templates? Simply put them in with all your other theme files and they’ll be ready to use. After you’ve done this, you need to create a page by going to Write > Page. In the right menu, there is a box called “Page Template” with a drop down menu. Your template should show up in this list, with the same name you named your template. Select it, save and you’re done!
What uses does this have? Plenty, like I said in my previous post. I’m going to dissect my Credits page, which is a fairly simple template I put together to display links like a bookmarks sytem. In fact, it uses WordPress’ blogroll to display the links and description.
First of all, I named my template file and called the header, like I explained above:
<?php
/*
Template Name: Credits
*/
?>
<?php get_header(); ?>
Next starts the layout off, or the primary content, and makes it display like all of my other pages:
<div id=”primary”>
<div class=”entrytext”>
Within my entrytext class, I’m using wp_list_bookmarks to call certain blogroll categories:
<p>Listed here is everything I’ve used to create the current design you see here today, including plugins I’m using.</p>
<ul><?php wp_list_bookmarks(’categorize=0&category=22&show_description=1
&title_li=&between=<br />&orderby=id&after=</li><br />’); ?></ul>
<p>WordPress Plugins being used are:</p>
<ul><?php wp_list_bookmarks(’categorize=0&category=23&show_description=1
&title_li=&between=<br />&orderby=id&after=</li><br />’); ?></ul>
See, php without the plugins. ;) Then I have simply finished off my template with the closing div tags and the usual template stuff (+ an extra sidebar since I have 2):
</div>
</div>
<?php get_sidebar(); ?>
<?php include (TEMPLATEPATH . “/sidebar2.php”); ?>
</div>
<?php get_footer(); ?>
If you like, you can download my credits.php template to have a look or use it for something, just don’t forget to adjust it accordingly, since not all layouts use “primary”, for example.
Great stuff Nyssa!
Hi Nyssa -
Thanks for a direct, and to the point tutorial that is very helpful. They have some coverage of this at WordPress.org, but their articles tend to be long and technical.
Let me know if you’d like to write a guest tutorial over at http://www.WebHelperMagazine.com
Yours -
Scott
[...] Nyssa for nyssajbrown.net, 2007. | Permalink | 2 [...]