Monday, January 26th, 2009 | 21 Days of WordPress Tips, News, WordPress
This post originally appeared by Tim Grahl in our blog at Out:think Group, if Tim is referenced in the comments, that's why.
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:
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.
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');
}
?>
May 6, 2009
Pinky says:
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.
October 14, 2009
FatBanker says:
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