Using wordpress template hierarchies to control CMS sites

If you’ve ever made a content manageable site for a less technically-savvy client, then chances are you’ve used custom templates. You’ll probably have one for the home page, and one for the content pages, and maybe one for the contact page. But what if the client wants to add a new page, and they use the wrong template? Hopefully they’ll realise quickly, and change to the appropriate template.
Some larger sites, however, end up having many layouts, and therefore many custom template, and they can become confusing to some users. This highlights the importance of naming your templates clearly and descriptively. It can help, though, to trim down the number of custom templates available to the client in the first place.
This can be done by utilising the template hierarchy built into wordpress. The full list can be found here:
This structure means that say, for instance, the home page (which is a static page) is going to have a unique layout complete with calls to action, etc, which don’t need to show up on any other pages. Instead of making a custom template file, you simply create home.php and fill it out exactly like you would the template page – just leaving out the Template Name comment at the top of the file. Now, you create the Home page inside the wordpress admin area, leave the template on ‘default’, and publish it.
When somebody visits your site wordpress automatically searches for the home.php file and applies it only to the home page. Any other pages will still use (for the time being) the index.php template. So, now you’ve created a custom template that the client does see, or need to see.
The same can be done for every other page by using either page-{slug}.php or page-{id}.php (e.g. page-contact.php). Personally, I would advise using the page id, as the user may change the page slug (if, for instance, they change the page name to contact us instead of contact). If you want to make it bullet-proof, you could always use both, though I’ve never found that necessary.
Make sure that page.php (the default page template) is styled as well, as any new pages the client adds will look like this one (since they won’t have their own page-{ID}.php).
This method is not just for pages, though. A quick look at the hierarchy shows that it can be done for archives, custom posts, categories, etc. If you want a particular category of posts to show up differently, category-{slug} or {id}.php will control that for you, with any categories that don’t have their own file automatically falling back to archive.php by default. The same goes for custom posts (though these MUST use single-{post-type}.php
But what if you want posts from a certain category to have their own single.php? At the moment, WordPress doesn’t support this very well, but there’s a good workaround if these posts are only going to be part of one category.
Firstly, create your single-{category-id} or {category-slug}.php files, as well as a single-default.php. Then, open single.php, clear it out, and paste the following php:

$post = $wp_query->post;
if ( in_category('11') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single-default.php');
}

Replace the ’11′ with the appropriate category id (which can be found by going to posts>categories>your-category in the wordpress admin area, and looking at the address bar). The above can be extended with as many else if lines as necessary, but make sure you always include else { include(TEMPLATEPATH . ‘/single-default.php’); } at the bottom!

Using this templating solution, the only time you really need to use page templates is when you want to give the client a choice of layouts (such as sidebar on the left, or sidebar on the right). This will result in much less confusing template choices for the client, and hopefully, less chance of them breaking the site you’ve made. Always ensure, though, that the default templates 1) exist and 2) have been styled!

Related posts:

  1. A Simple HTML5 Template Tutorial
  2. Magento Template Path Hints
  3. How To Speed Up Your WordPress Blogs
  4. Adding a custom widget area in WordPress

Leave a Reply


Contact Us Today...

If you have any questions about our services or want to arrange a free no obligation consultation contact us today or call 02920 290 080 for Cardiff and 01179 000 482 for Bristol.

Bit Torrent Study Discovers Most File Sharers Are Now Monitored

Ever heard of torrents or used Bit Torrent to download movies, music or shared files over the internet? Researchers have discovered that anyone who does use Bit Torrent to download files over the World Wide Web will most probably be monitored. Studies were carried out by Birmingham University and they have discovered that if someone [...]
» more

Cardiff: 02920 290 080 Bristol: 01179 000 482