(+84) 463.28.7979

How To: Use WordPress Conditional Tags to Hack Your Theme


By using simple conditional tags – it’s pretty easy to add some very basic hacks to your WordPress theme to have more control over what’s displayed when.

Here are some things you could do with a conditional tags:

  • Display something only on certain pages
  • Display something only on certain categories
  • Display something in header and footer only at certain times
  • Display something only on sub-children of particular pages
  • Display something only in the WP dashboard
  • Display something in the sidebar only when certain conditions are met
  • Do something only when there’s a “sticky” post
  • Do something only when a “page template” is used
  • Do something only for “author pages”
  • Display something only on search or 404 pages

Let’s say you want something to display only on the homepage, or just category pages, or maybe just your 404 (not found) page – it’s quite easy to do. You don’t have to be a hardcore programmer (I’m not for sure) to implement these very simple theme hacks.

Where to Use Conditional Tags

It all depends on your your theme is structured. I’ve seen WordPress themes that a single “index.php” file handle just about everything, and other ones that use only the home.php, index.php, single.php, archive.php, and category.php files. You could handle everything with a bunch of code in one file if you want using conditional tags, have individual files for each thing, or any combination in between.

Most themes I’ve encountered usually have an index.php and a single.php only. If you want to know what pages WordPress looks for first in a theme before defaulting to the “index.php”, read the official WordPress Template Hierarchy page.

Conditional tags are great because you can use them both in and out of the loop. You can use them directly in theme pages, but you could also use them in your header, footer, comments, and sidebar files. Actually the sky is the limit, and you have only your own imagination to limit you!

Conditional Tag Examples

The worst thing I see on most posts about conditional tags is that they don’t have a lot of examples for you to draw from – so I’ll try to give you a few ideas to get you started…

If This is XYZ page

One of the most common ways to use a conditional tag is to add a filter of sorts to tell WordPress “if I’m on ‘XYZ’ WP page – then do this. The most common reason would be maybe to show certain things (ads, text, messages, graphics, scripts, flash) in certain places.

For example, let’s say you want to display a message to visitors on your home page only…

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

Welcome, you will only see this message on my homepage!

<?php } ?>

replace “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, “is_date”, “is_archive”, or “is_attachment” to make your message show up on nearly any WordPress page. Remember, once the condition is met, you can “do” anything – from including a file to showing special graphics, running a script, anything! You could use this condition for example to show an ad on just your homepage, or just single pages.

If this is XYZ page show this, else show that

The nice thing about conditional tags is the fact that you can have as many conditions as you want…like this:


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

<p>Show this!</p>

<?php } elseif (is_single()) { ?>

<p>Show this instead!</p>

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

<p>Show something different!</p>

<?php else { ?>

<p>Show this if no conditions are met</p>

<?php } ?>

OR – you could even structure it in such a way that you lump some conditions together like this:

<?php if (is home() || is_single() || is_category() || is_page()
|| is_archive() { ?>

<p>Show this on all those pages!</p>

<?php } ?>

The double-pipe or || in the code signifies “OR”, so WordPress knows, if this is home, or a single page, or a category page, or a “page” page, or an archive page – then so something.

Show Everywhere, Except…

Sometimes you want to show something everywhere possible except just one or two places…

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

<p>Show this everywhere!</p>

<?php } ?>

With this code we just say if it’s “home” do nothing, else show do this. You could add multiple conditions (exclusions) to this using the || OR operator as in previous examples.

Getting even more specific

One thing I hadn’t mentioned was that you could pass additional parameters to the conditional tags for even finer grained control. For example, instead of targeting are single post pages with “is_single”, you could actually target just one using any of these formats:

is_single(’25′) // uses posts ID
is_single(‘Title of my post’) //uses the exact title of the post
is_single(‘title-of-my-post’) //uses the permalink of the post
is_single(array(25,’this title’,’this permalink’)) //uses when any of the 3 are true

You can use similar parameters for paged pages, template pages, categories, tags, etc. The official WordPress Conditional Tag page in the Codex lists them all.

Force WordPress Functions for Certain Conditions

Have you ever not wanted to add people to your blogroll because their link will display on EVERY SINGLE page of your WP powered site? That’s an easy hack with conditional tags, because you could hack your sidebar to display your blogroll ONLY on your homepage like this:

<? php if (is_home()) {
wp_list_bookmarks();
}
?>

You could modify this to display just about anything in the sidebar for whatever conditions you want. Let’s take this a bit further though – let’s say that maybe I want a special header or footer when certain conditions are met? You can do that too…

<? php if (is_home() || is_single() || is_page()) {
get_header();
}
elseif (is_category() || is_tag()) {
include (TEMPLATEPATH . '/header2.php');
}
elseif (is_404() || is_search())
include (TEMPLATEPATH . '/header3.php');
} ?>

Using that example code you could have as many different headers, footers, or sidebars as you wanted and you could include them for whatever conditions you specified. Just replace get_header with get_footer or get_sidebar, and edit the rest include the files you want.

Using Conditional Tags to Change Styles

So far my conditional tag examples have been to show you how code conditions to do this or include that. Another very simple (and powerful) was to use conditional tags is to just use them for coding style. You could have any element (paragraph, div, heading) change colors or font size or anything related to style when certain conditions are met.

Let’s take a very simple example, maybe your pages have content contained within one div like this:

<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>

In most themes, that main “div” is styled the same way on EVERY single page of the theme. Maybe you want it styled one way for your homepage, but another for paged pages, single posts, archives, etc. What you do in this case is to write different classes in your stylesheet for each and call them something like “single”, “archive”, and “paged” – and then you code it like this:

<div id="content" <?php if (is_home()) { ?>
<?php } elseif (is_page()) { ?>
<?php } elseif (is_single()) ?>
<?php } elsif (is_archive()) { ?> <?php } ?> >
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
</div>

You use any variation of this to control any element of your theme at will under nearly any condition. This would also be a great way to control your Post title and meta and have it display different things on different parts of your WordPress powered site.

Conclusion

I think mostly that the power of conditional tags is widely underutilized. Many of us just take for granted the way a theme works and looks, and forget that with just a few strokes of code and some imagination – we can change just about anything under the sun in our WordPress theme! I’d like to see some comments on what kinds of things you’ve used conditional tags for in your WordPress site (code samples welcomed!).

This post was submitted by John Pratt, who writes about how to Make Money Blogging. His latest post is How to Build a WordPress Craigslist.

Leave a Reply

Your email address will not be published. Required fields are marked *

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>