WordPress Conditional Tags

What the Weatherman Can Teach You About WordPress Conditional Tags

How many times have you flipped on the morning news to get the day’s forecast? Sure, you can guess at the basics (if it’s a summer day in Texas, it’ll be hot, right?), but we still like the extra details a meteorologist can offer. 

Is it raining ? Are water restrictions in effect? Will I die of heat stroke if I run after work? Is it raining men?

Each of these details give us the information we need to make the best decisions for our day. So, if it’s going to rain, I’m going to cancel pool time and head to the movies instead. If we’re hitting a record high, I’m going to suck it up and jog on a treadmill instead of pounding hot pavement.

In much the same way, we can use WordPress conditional tags to show when certain situations ring true and then use that knowledge to change the default behavior. Even better, WordPress conditional tags are always 100% accurate, unlike meteorologists. 🙂

Rain Down Some Details, Carrie

You’ll typically find WordPress conditional tags used in conjunction with PHP conditional statements (i.e. if/else). It’s a big conditionals party, really.

You can use these conditional checks in your theme’s functions.php file or in individual theme template files. We’ll talk a little more about that later.

Start with the Basics

While my WordPress site might not care if it’s raining, perhaps it does care when a single post is displayed. Here’s a basic example of how WordPress can know if that’s true:

if ( is_single() ) {

echo ‘Hey there, singleton!’;


The is_single() tag returns true if any single post, attachment, or custom post type page is being displayed. This bit of code isn’t very useful, but it does demonstrate a PHP conditional statement (if) with a WordPress conditional tag ( is_single() ).

A Real Example

You can use these conditional checks anywhere in a theme, but not every place makes sense. For example, if I’m working on a single page template, it wouldn’t make sense to ask the page if it’s single (i.e. is_single() ). That’s like asking a drop of rain if it’s wet.

Instead, we should ask when we don’t know the answer, like in our theme’s functions file. Here’s an example:

Write it in code:

if ( is_single() && is_author('Carrie') ) {

$classes[] = ‘giddyup’;
return $classes;


Say it in English:

If the post being viewed is single AND Carrie is the author, then apply a special body class called ‘giddyup’.

The Bigger Picture

If we stuck the code above in our theme’s function file, it would do a BIG FAT NOTHING. Why? Because to “interfere” with the default WordPress output, we need to hook into the code using an action or a filter. (If you’re using a theme framework like Genesis, you’ll have even more actions and filters at your disposal).

In order for our code to work, we need to make a function out of it and then use a filter to tell WordPress where/when to pull the trigger on our code.

Write it in code:

add_filter( 'body_class', 'my_body_class' );

function my_body_class( $classes ) {

if ( is_single() && is_author(‘Carrie’)() ) {

$classes[] = ‘giddyup’;
return $classes;


Say it in English:

At the point where WordPress assigns a class to the <body> tag, add a custom body class of ‘giddyup’, but only if it’s a single post authored by Carrie!

Learn More About WordPress Hooks

Yours truly has a course on LinkedIn Learning that will make you 100% confident working with hooks! Check out my course WordPress: Action and Filter Hooks.

I Wanna Get Absolutely Soaking Wet

This is a mere sprinkling of what you can do with WordPress conditional tags. If you want to delve into plugin development or theme customizations, you’ll need to spend time immersing yourself in conditionals.

Did I overdo the water words? You’re welcome.

Here’s a link so you can bookmark the official codex page: WordPress Conditional Tags. Visit it and visit it often.

Happy coding!

Image credits for “It’s Raining Men Wallpaper” to Jordan LaPointe.

19 thoughts on “What the Weatherman Can Teach You About WordPress Conditional Tags”

  1. Pingback: Modify Yoast Breadcrumb Text for Parent and Child pages

Leave a Comment

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

Carrie Dils uses Accessibility Checker to monitor our website's accessibility.