How to Use WordPress Conditional Tags

This article is deprecated! Any technical information contained herein likely refers to software versions that are now obsolete. Please visit our blog home page for current updates.

Let’s say you want your Recent Posts to appear on one specific page of your WordPress website. To accomplish that, you would create a special function and hook the Recent Posts into place.

I know those two words—function and hook—are scary, but bear with me. It’s not as hard as you think… especially when you follow this step-by-step tutorial.

So, how can you get Recent Posts to appear on one page, but not another? Well, you would use what nerdy programmers like myself call conditional statements, which is really just tech speak for “if, then statements.”

How do “if, then statements” work? Well, quite simply, if you’re on page 1, show Recent Posts, else, if on any other page, don’t show Recent Posts. Logically, it makes sense, right?

The question is, what’s the easiest way to incorporate these conditional statements into the Thesis Theme Framework? Keep reading.

The Power of the Thesis Custom Functions File

One of the great things about Thesis is the custom_functions.php. Instead of modifying or duplicating information across multiple files, which is required in some themes, you can write one function, use Thesis Hooks to get it where you want it, and bam, you’re done. But, now, let’s get down to business.

How to Use WordPress Conditional Statements in the Thesis Theme Framework

From this point forward, you’ll be working with your custom_functions.php file. Please be careful. If you paste or tweak the code in incorrectly, you’ll need access to your FTP to fix it. Also, please be sure to backup your custom_functions.php before you get started; that way, if you make a mistake, you can revert back to your original with no problems.

To start, let’s look at a basic Thesis function that adds a list of recent posts to the sidebar. To do that, you would include the following code in your custom_functions.php file:

function recent_post_list() { ?>
    <li id="recent_posts" class="widget">
    <h3>Recent Posts</h3>
    <?php query_posts('showposts=5'); ?>
    	<ul class="recent">
		<?php while (have_posts()) : the_post(); ?>
		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
		<?php endwhile;?>
		<?php wp_reset_query();?>        
        </ul>
    </li>
<?php }
add_action('thesis_hook_before_sidebar_1', 'recent_post_list'); // Recent Posts

Real simple. But let’s say you only want that to appear on the main page of the site? Adding 1 line is all it takes

function recent_post_list() { 
    if(is_home()) { ?>
        <li id="recent_posts" class="widget">
        <h3>Recent Posts</h3>
        <?php query_posts('showposts=5'); ?>
    	    <ul class="recent">
		<?php while (have_posts()) : the_post(); ?>
		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
		<?php endwhile;?>
		<?php wp_reset_query();?>        
            </ul>
        </li>
<?php
    }
}
add_action('thesis_hook_before_sidebar_1', 'recent_post_list'); // Recent Posts	

That’s it!

Now, if you take a look at the code, do you see the second line of the function that includes the conditional tag is_home? Also, take note of the 2nd bracket at the very end. Forgetting that will cause the function to error.

Ok, so now say you want it everywhere except the home page. Well, WordPress conditional tags allow for both inclusion and exclusion. Here’s the code for your custom_functions.php file:

function recent_post_list() {
    if(!is_home()) { ?>
        <li id="recent_posts" class="widget">
        <h3>Recent Posts</h3>
        <?php query_posts('showposts=5'); ?>
    	    <ul class="recent">
		<?php while (have_posts()) : the_post(); ?>
		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
		<?php endwhile;?>
		<?php wp_reset_query();?>        
            </ul>
        </li>
 <?php
    }
}
add_action('thesis_hook_before_sidebar_1', 'recent_post_list'); // Recent Posts

Adding the exclamation point causes the function to trigger everywhere other than the home page.

So, are you getting the idea? Here are a few other examples of conditional tags that you can use in your Thesis file:

if (is_home() || is_archive())

This would trigger either on the home page or on any archive page (category, tag, etc). Putting the two solid lines in between makes each conditional tag stand alone.

if (is_single() && in_category('awesome'))

This would trigger ONLY on single posts in the category named “awesome”.

if (is_page('about-me')) 

This would trigger on just the “about me” page. Take note, when I target a page, you have to use the page-slug, which is what appears after .com/

if (is_category(array('awesome','tutorials')) 

Whoa, I included something else here, and it’s called an array. What’s the deal with Arrays? Well, when you include an array, it allows your code to target several different things at once.

For example, in the above example, the recent posts would trigger on the category archive page for “awesome” and the category archive page for “tutorials.”

*A note about categories* Deciding when to use is_category and in_category can be tricky. Usually, it takes some trial and error to get it fine-tuned to that level of detail. As a rule of thumb, use in_category in conjunction with is_single.

Can You Add Anything Other Than Recent Posts?

Absolutely. All you have to do is replace the HTML, the code that falls between the the opening <li id="recent_posts" class="widget"> and closing </li>. For example, you could include the HTML for your ad tags, and then, target your ads to specific pages. It’s that easy.

The Bottom Line

That wasn’t so hard, was it? If you have any questions, refer to the WordPress Codex to see all the different tags that are available (there are many). Or, you can leave a comment below and I’ll pop in here to answer some questions!

About the author: Norcross is a full time freelance WordPress developer and tattooed web nerd in Tampa, Florida. You can check out his work here, or you can see random swearing and pictures of an awesome “littleman” on twitter. He’s also a featured Thesis Designer.