WordPress as CMS: Extra template files
Previously I wrote about the Template file hierarchy in WordPress and what your options are. However, when you’re using WordPress as a content management system (CMS), you’ll often find yourself in a position where you need extra template files outside of the normal hierarchy.
In this post I’ll describe two common examples and how to accomplish them. But first, here’s the piece of code we’ll be using:
<?php
include(TEMPLATE . '/template-file.php');
?>
This bit of code is extremely useful as it can be used in any template file to include any other template file. Here’s the two most common uses:
Category Sidebars
If you would like to show a different sidebar based on which category the current post is in:
<?php
if(in_category(1)) {
include(TEMPLATE . '/sidebar-1.php');
} else if (in_category(2)) {
include(TEMPLATE . '/sidebar-2.php');
} else {
include(TEMPLATE . '/sidebar-post.php');
}
?>
This could be useful to show a different FAQ for a product category, a different set of widgets if it’s the News category, etc.
Homepage Header
Since the homepage is often a separate layout from the rest of the site, you may want to have a header-home.php template file that is used on the home page and then the default header for the rest of the site.
So you can continue using the default get_header() call on all the template files, here’s the best way to go about it:
1. Duplicate your header.php file and name it header-default.php
2. Create a header-home.php file and insert your markup for the home page header
3. Open your header.php file and delete everything (only after step #1 of course) and add this code:
<?php
if(is_front_page()) {
include(TEMPLATE . '/header-home.php');
} else {
include(TEMPLATE . '/header-default.php');
}
?>
Post filed under News
May 6th, 2009 at 9:22 am
Tim, this is great information.
I was wondering how to make the matching header for blog and site. I guess your information will help me. I am not a coder but I can tweak a bit.
Thank you very much Tim.
May 24th, 2009 at 4:21 pm
Great tips, thank you.
But, why we keep playing with the wordpress code, and trying to make something that we can have it from other open sources, wordpress is for blogging nothing less, nothing more.
I even believe some ideas are great when we play arround with the code, but shouldnt we respect the feact that wordpress is just for blogging?
July 12th, 2009 at 9:05 am
I found your blog by chance . but i have to say that it’s great blog very useful information and very interesting subjects just greetings and good luck
i’m not going i will be always checking for updates.I’m very interested in CMS and all its related subjects.
July 12th, 2009 at 9:06 am
Very useful information. Thanks for this. You got a great blog .I will be interested in more similar topics.I’m very interested in CMS and all its related subjects.
October 14th, 2009 at 1:15 pm
Thanks for a great post. Learning WordPress and looking to design a landing page that will have a different format to other pages. This post is great information.
FB