WordPress Conditional Tags (And Snippets) for Beginners

One of the best features of the WordPress could be the conditional tags. It allows you to tell the code to act differently in specific situations. For example, you can check if the user is using Windows or Mac, and display different content based on the systems. You can also redirect to post if search query only returns single result. You name the situations, the conditional tags can recognize them all!

Despite its flexibility on determining actions based on different situations, it’s also extremely easy to learn, and there are even tutorials and resources spread over the web for you to actually master it. That said, in this article we’ll go through a detailed introduction about the conditional tags, how they work and when to actually use them.

In the last section of the article we’ll also show 10 useful snippets for you to achieve the most with conditional tags, so get them all to make your WordPress site acts more intelligently to unique situations!

If (Statements)

With PHP if statements you can ask if something is true or false, 1 or 0. If your statement is true, your code will be executed, and if it’s false nothing will happen, depending on how you decide the actions in the conditional tags. Check out the example, and I’m sure you’ll understand what I’m talking about.

<?php
if(10 == 10):
echo 'This is true, and will be shown.';
endif;
if(10 == 15):
echo 'This is false, and will not be shown.';
endif;
?>

You can also use elseif which lets you add another statement, and else that will be executed if your first statement is false.

<?php
if(10 == 11):
echo 'This is false, and will not be shown.';
elseif(10 == 15):
echo 'This is false, and will not be shown.';
else:
echo 'Since none of the above is true, this will be shown.';
endif;
?>

That’s all you need to know about if statements for now, let’s get into WordPress conditional tags! However, if you want to dig deeper into PHP if statements, head over to php.net for reference.

How Conditional Tags Work?

When using the native WordPress function like is_home(), you simply ask WordPress if the user is currently on the home page or not. WordPress will then answer with 0 for no, and 1 for yes.

<?php
if( is_home() ):
echo 'User is on the homepage.';
else:
echo 'User is not on the homepage';
endif;
?>

For a complete list over WordPress conditional tags you can visit their codex.

Combining statements

There are cases when you might want to check more than one statement. This is easily done by using AND and OR.

<?php
if( is_home() AND is_page( 5 ) ):
echo 'User is on the homepage, and the home pages ID is 5';
endif;
if( is_home() OR is_page( 5 )):
echo 'User is on the homepage or the page where the ID is 5';
endif;
?>

When To Use Conditional Tags?

Conditional tags are great when you want to change your content depending on the answers of question relevant to your site. Is the user logged in? Is she using Internet Explorer? Is there any post to be shown?

To get an example of conditional tags in use, we can look into Twenty Eleven’s (the standard theme in WP 3.2) index.php, line 20.

This checks if there is any post to show, and if the answer is no, the search field is displayed.

Below is another example of WordPress conditional tags:

if( is_admin() ):
# User is administator
endif;

if( is_home() AND is_page('1') ):
# The user is at the home page and the home page is a page with the ID 1
endif;

if( is_single() OR is_page() ):
# The user is reading a post or a page
endif;

if( !is_home() AND is_page() ):
# The user is on a page, but not the homepage
endif;

10 Useful Conditional Tags

The conditional tags available in the WordPress codex page are pretty limited to the big parts of WordPress, like posts, pages and such. There are, however, a lot of small and useful statements available if you look around the web.

Check if user is logged in

This will be a handy snippet if you have a blog with users registered, as it checks whether your user is logged in or not.

Disable Google Analytics for logged in users

If you are using Google Analytics and you only want to track the visitors other than your authors and writers, you can probably use this snippet to achieve the purpose. Be sure to change UA-XXXXXXX-X to your Google Analytics ID.

Check if last post

The chances are if you’re using separators between your posts you probably don’t want to include it into the last post of the page. Include the conditional tag below into your loop where you want to display something only if it’s on the last post.

Check if current user can…

There are times when you want to know the role of the users, for example you only want certain links (edit, etc) to be displayed for authors. The function current_user_can() works like what mentioned above, and here is the code:

Check if user is coming from StumbleUpon

StumbleUpon is a great social media which is able to attract traffic to your site. Here’s a trick to attract the Stumblers: display a special message to welcome them by using the conditional tag below which checks if the user comes from StumbleUpon.

Editor’s note: This post is written by Filip Stefansson for Hongkiat.com. Filip is a web developer & WordPress enthusiast from Sweden. He is also the founder and editor over at WP-Snippets, and co-founder of Pixby Media AB.