Using Conditional Tags – The Basics

Some more basic information about using conditional tags in WordPress template files.

A site visitor recently asked me about conditional tags. The best resource is in the WordPress Codex, on the Conditional Tags page. I reference this page so often that I have it bookmarked.

The only problem with the page is that the examples are very complex. You can actually break down the structure of a conditional statement to these few lines:

< ?php if (is_home()) { ?>

< ?php } elseif ( is_category('2') ) { ?>

< ?php } else { ?>

< ?php } ?>

I even have a plain text file with this code sitting in it, waiting for me to reference and use it.

Let me explain in a bit more detail how this might work. Here’s an example.

Suppose you want to use conditional statements in a sidebar to display different content depending on what the site visitor is looking at. On the Home page, you want to display your company logo. On a category page, you want to display a description of the category. On any other kind of page, you want to include a search form. And then on every kind of page, you want to list links and include the same kind of stuff everyone else includes.

So the first thing you need to do in your sidebar is to test to see if the page being displayed by WordPress is the Home page. You do this with the is_home() conditional statement, like this:

< ?php if (is_home()) { ?>

Right after this bit of code, you’d include the code to display your logo.

Now you need to test to see if WordPress is displaying a specific category page. You could do it with this line of code:

< ?php } elseif ( is_category('2') ) { ?>

which tests to see if WordPress is displaying category 2. (If you didn’t care which category was being displayed, you could just use is_category().) You’d follow that up with the code for what you wanted to display on a category 2 page only.

Now you might want to test for another specific category page. You could use this code:

< ?php } elseif ( is_category('5') ) { ?>

which does the same as the previous code but looks for category 5. Follow that up with everything you want to appear on a category 5 page.

Repeat this for all the tests you want to do.

Now you’re done making specific tests and want to display stuff that should appear on all pages except the ones you tested specifically for. You could enter:

< ?php } else { ?>

which says that if it doesn’t meet the previous conditions, WordPress should do what comes next. You’d follow that code with what you want to appear on all pages except those you tested for.

Finally, you need to end the conditional statements. You can do that with:

< ?php } ?>

This tells WordPress to stop evaluating and acting on conditional statements. You’d follow up this code with anything you want to appear on all pages, no matter what type.

Probably the best way to learn this is to experiment with it. Set up conditional statements in a template file and then load the corresponding pages in WordPress to see what happens. Be sure to include the < ?php } ?> at the end of the conditional tests — failure to do so will probably lead to parsing errors.

Once you get the hang of this, you can create nested conditional statements that evaluate multple conditions before acting. I did that for my Flying M Air site, which I’m explaining in a series of articles on this site about using WordPress as a CMS. But before you attempt anything as complex as that, make sure you’ve mastered the basics. Debugging this stuff is no fun when the code is too complex to understand.

Questions? Comments?
Share them here!

Comments are moderated. I encourage comments that further the discussion, but will not tolerate rudeness or spam. Your email address is never shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>