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" class="main">
<?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()) { ?> class="home"
<?php } elseif (is_page()) { ?> class="paged"
<?php } elseif (is_single()) ?> class="single"
<?php } elsif (is_archive()) { ?> class="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!).

 

  • Leave a Comment
  • How To: Make your WordPress Search Results Unlimited

    Sometimes you may not want your search results to be limited by the confines of the standard WordPress Loop. This is a quick code hack to allow a search to return unlimited results, altering the standard WordPress Loop by using a custom query. You can do this in a few different ways. If you have a search template, in search.php you can simple add the following line of code above your Loop.

    Find:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    Add:

    <?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    Make sure you put this code in your search.php only, unless you want unlimited posts on your index or archive pages. The -1 you see can be changed to any positive integer to limit the posts to a number you choose as well.

    If you don’t have a search.php in your theme, the next level down in the Template Hierarchy is your Main Index Template, or index.php. You can use a conditional tag for the same effect.

    For this we’ll use the same code as above, except wrap it in the is_search() conditional tag, like so:

    <?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    You can use this technique to change the standard Loop limitations of archives, categories, tag pages, and even your main index template – but it would probably be easier to simply change your reading settings for that.

  • Leave a Comment
  • Improve Your Sites Behavior With WordPress Conditional Tags

    If you are a WordPress designer, are you using conditional tags in your themes? For those unfamiliar with them, conditional tags are snippets of PHP code you can easily use to set up conditions where the code is used. The most common use for these would be for your sidebar or your pre/post meta sections of your posts where you display your post details. With conditional tags, you can display certain functions only on pages of your choice. A couple of months ago we talked a little bit about WordPress conditional tags on this site and when to use them, but we didn’t get into a great amount of detail.

    Leland over at Theme Lab has been working on some great WordPress guides lately, and one of his recent posts that caught my attention is the Ultimate Guide to WordPress Conditional Tags. This post provides a more thorough explanation of conditional tags and how to use them. If you’ve been wanting to improve your coding, this would be a great post to check out.

  • Leave a Comment
  • WordPress Hack: Using WordPress Conditional Tags

    Have you ever noticed how some pages on blogs behave different than on other pages? One of the most under-utilised features of WordPress is the wide variety of conditional tags available in WordPress. Through the use of conditional tags, you can instruct certain plugins, pictures, or code to only appear on designated pages.

    Recently I ran across a post by the Undersigned explaining Conditional Tags in WordPress, which appears was written in 2006 but is still valid with current versions of WordPress.

    Here is a list of the conditional tags available:

    • is_home()
    • is_single()
    • is_page()
    • is_category()
    • is_author()
    • is_date()
    • is_year()
    • is_month()
    • is_day()
    • is_time()
    • is_archive()
    • is_search()
    • is_paged()
    • is_404()

    One of my favorite places to use conditional tags is in the post meta area. For example, on most pages I want the comments button to display, but I don’t need it to appear on the single page because the comments are displayed on that page. At the same time, I like having an edit button on the single page, but I don’t need it on any other pages.

    Here is the code I use for the above example on one of my sites:

    <?php if (is_single()){ ?> <?php edit_post_link(__("*Edit*"), ''); ?> <?php } else { ?> | <?php comments_popup_link('0 comments', '1 comment', '% comments'); ?> <?php } ?>

    The bold code is the conditional tags I’ve set up.  You can see from the above code that I am telling WordPress to only display an Edit button on single pages, and on all other pages display the comments link.

    What other good uses have you found for conditional tags?

  • Leave a Comment