Secrets of the Conditional Tag Revealed: How to Gain More Control Over Your WP Templates

This is an exclusive guest post by , posted on August 25th, 2008.

[ ~{*}~ ] More and more these days, we are all finding WordPress being used as a content management system. It shouldn’t be too tough to see why — highly customizable, a community growing in size and knowledge, and a plethora of options in the way of plugins and simple yet highly effective PHP edits. Thanks to these, you have access to an open source script that allows you to show what you want, when you want, where you want, and how you want in virtually no time. But what happens when you only want a simple variation from one page to the next?

Enter: the conditional tag.

What is it?

Conditional tags, in short, say that if these certain requirements are met, then put this code in the page. I’m sure that you can imagine the level of control this allows you to have over your themes. If you only want to display your blogroll on the home page? Use a conditional tag! Want to stick AdSense in all your posts except for the ones categorized as “sponsored posts?” Use conditional tags! Do you want to avoid creating a whole new file in your template for a 404 error page? Conditional tags to the rescue! Of course, do note that one of the great things about WordPress is that there are several different ways to do everything, but I’ve never heard anyone complain about adding an extra tool to the toolkit. ;)

The Setup

Let’s set up a scenario that calls for a need for Conditional tags. Let’s say that you’re creating a website where you want different content to show on your sidebar for different parts of your website. You have three different sidebars that you want to display on your website: the first one is housed in primary-sidebar.php; the second one in secondary-sidebar.php; and the third, tertiary-sidebar.php. You want primary-sidebar.php to appear on your “home”, “archives”, and “contact” pages. You want secondary-sidebar.php to appear whenever a single post is being displayed. (I know you could always just add another sidebar.php file, but if the variation between the two would only be slight, what’s the point?) Lastly, you want tertiary-sidebar.php to be shown everywhere else.

What do conditional tags look like?

<?php if(is_home() || is_archive() || is_page('contact')) { ?>
	<?php include(TEMPLATEPATH.'/primary-sidebar.php'); ?>
<?php } elseif(is_single()) { ?>
	<?php include(TEMPLATEPATH.'/secondary-sidebar.php'); ?>
<?php } else { ?>
	<?php include(TEMPLATEPATH.'/tertiary-sidebar.php'); ?>
<?php } ?>

Ladies and gentlemen, that is a conditional tag.

Allow me to explain.

Line 1: This is your opening statement. This basically sets up the “requirements” that I mentioned before, and uses an open bracket “ { ” to denote the start of the conditional content. Line 1 in this example basically says

If this page is the home page, ANY kind of archive (that includes when posts are sorted by category, by author, by date, or by tag), or any Page named ‘contact’, show the following content.

Line 2: This is the content that you want to display when the page being viewed meets those requirements. It doesn’t have to just be an included file, it doesn’t have to be one line, and it certainly doesn’t have to be short.

Line 3: This line closes the brackets from line one, therefore ending that “option.” Following in line with what Line 1 said, this one says:

If the previous requirements aren’t met, here are some new ones. If the page being viewed is a page that displays a single post, show the following content.

Line 4: Here, we set up the content to be shown if Line 3 is applicable.

Line 5: At this point, we’ve done just about everything we wanted to do, and the only thing left is to set up the rule that says “show tertiary-sidebar.php on everything else.” Line 5 sets that up, and Line 6 declares the content to be shown.

There are a couple of things to note here. First, take note of the double lines used in line 1 – seen as “||” – which are used to say “or.” So, the line translates to “If this page is the Home page OR an Archive OR is a page named ‘contact’.” If you wanted to say “and” instead of “or,” you would use double ampersands “&&” in place of the lines. Second, if you were uncomfortable using the name of a page as opposed to a page id, you can say is_page('231') instead of is_page('contact'), with “231” being the page_id for your contact page. Third, if you wanted to add MORE variations, you would duplicate Lines 5 and 6, changing the rule and the content to suit your needs.

In conclusion, I hope I’ve at least enlightened you to go seek out more information about this powerful little piece of code that gives you that much more control over what your templates do for you. There are numerous possibilities, and I hope I was able to multiply those for you at least by a little!