Conditional Tags are one of those things that you can leverage on your WordPress site whether you are an ace in coding or just have enough knowledge to install a plugin. It’s amazing what a simple piece of PHP can do for your WordPress site. They work well with BuddyPress too.
But before we begin we really should know what conditional tags are, and the simple answer to that question is that a conditional tag is basically a yes/no question. When you use them, they return either a True or False response, and that response determines what happens, based on your instructions.
Here is a simplified breakdown of what a conditional tag looks like:
if ( define a condition) {
// Then do stuff
};
What happens here is very straightforward. IF the condition specified In the parentheses ( ) is true, THEN do whatever is laid out in the curly brackets { }.
if ( it’s hot outside) {
echo ‘Have a cold drink’;
};
So in our example, if it is hot outside, then you should have a cold drink…sounds like decent advice…
Let’s reverse that!
Conversely, if the condition in the parentheses is not met, then the rest is ignored.
We can reverse that and make this work so that if a condition is not valid something happens simply by adding an “!” mark to the beginning of our condition like this:
if ( ! define a condition) {
// Then do stuff
};
So in this example, because the condition in the parentheses is not true, we want to do the stuff set out in the curly brackets.
Going back to our drink example we could do something like this:
if (! It’s hot outside){
echo ”have a warm beverage”;
};
At first glance, if you were to read that statement without your developers cap on, it might seem unusual to have a warm beverage when it’s hot outside, but that ‘!” at the beginning turns everything around and the statement then makes sense.
So this works very well when you’re just checking for a single condition but what happens when you want to check multiple things and do something when one of the conditions is met?
Let me introduce you to the ELSEIF statement…
The ELSEIF statement is very similar to the ELSE statement and is actually an extension that allows you to check multiple conditions and perform an action when one of the conditions is met. Here is what it looks like in theory;
if( original_condtition){
// Do something here
} elseif ( second_condtion){
//do this instead
};
Breaking that down, it simply states that if the first condition is true, then do as instructed. If the first condition isn’t true, move on to the second condition and if that’s true, perform that action instead. You can add as many elseif statements as you like and I’ll show you an example using multiple elseif statements with WordPress code in the next section.
Combining Statements
So it’s great that you can check a condition and then perform an action , but what happens when you want to check multiple statements? Say hello to your new friends AND and OR.
As an example lets show the same unique text at the beginning of the home page but only if the home page has the id of 3:
<?php
if (is_home() AND is_page(3)):
echo 'Welcome to the home page. Glad you could make it';
endif;
?>
And if we want to make the condition true if either of the conditions are met:
<?php
if (is_home() OR is_page(3)):
echo 'Welcome to the home page. Glad you could make it';
endif;
?>
(Note: instead of AND, we could have used ‘&&’, and instead of OR we could have used ‘||’.)
List of Frequently used WordPress tags
1. is_page()- applies a condition to a specific page, e.g. ” Contact”. You can use the ID, title,or slug/name.
is_page(‘3’) or is_page(‘Contact’)
2. is_home()- applies a condition to the home page.
3. is _category()- applies a condition to a specific category.e.g. “Football”. Same as the is_page() example, we can use ID, title or slug/name
is_category(‘football’)
4. is_single()- applies a condition to single posts or attachments
5. is_tag()- applies a condition to a tag archive page. Similar to a category archive page.
6. is_author()- applies a condition to an author archive page
7. is_archive()- applies a condition to archived pages
8. is_404()- applies condition to an HTTP 404: Not Found error page
9. is_main_site – is main site in a Multisite setup
WordPress Specific Example
Say you are building a site for a newspaper and they would like to display a different banner image for each section of the newspaper to help visitors identify which section they are currently reading. By using a simple set of conditional tags, you can easily accomplish this.
Our newspaper has four different sections we want to identify:
- Lifestyle
- Entertainment
- Sports
- And a subsection of Sports –Football
( it’s a small, but well-written newspaper)…
So in order to display a banner image that’s different for each section we need to do some code work in the header.php of our theme. Using a code editor, look for the line
<header id=”masthead” role=”banner”> as we will be working in this general area. Now depending on what theme you are using ( I used twentytwelve for this example) you will want to first remove the following code:
(Reminder, if you are doing this on a live site, you will want to create a child theme so that your changes don’t get overwritten when you do upgrades on the theme)
Remove this section, which is the default header image code:
<?php $header_image = get_header_image();
if ( ! empty( $header_image ) ) : ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
<?php endif; ?>
and replace it with this code:
<?php if(is_page('lifestyle')):?>
<img src="http://yourdomain.com/wp-content/uploads/2013/03/lifestyle1.jpg">
<?php elseif (is_page('sports')):?>
<img src="http://yourdomain.com /wp-content/uploads/2013/03/sports.jpg">
<?php elseif (is_page('entertainment')):?>
<img src="http://yourdomain.com /wp-content/uploads/2013/03/entertainment.jpg">
<?php elseif (is_category('football')):?>
<img src="http://yourdomain.com /wp-content/uploads/2013/03/football.jpg">
<?php endif;?>
While it may look intimidating, it really shouldn’t be as all we are doing is working our way down a list and when one of the conditions are met, ( in this case a specific page being displayed) a condition is met ( show the appropriate image). If the condition wasn’t met, it wasn’t the right page, then keep working down the list until it found the proper condition ( the proper page) and display the image indicated for that page.
Pretty simple…and pretty nice looking too
So what happens if you want to do something other than what we have shown here? All you need to do is replace the condition (is_page(‘pagename’)) with any of the conditional tags found here in the Codex.
Say for example you wanted to put the text ‘Exclusive Article’ at the top of every single post, then all you would need to do is:
<?php
if(is_single( )){
echo ‘Exclusive Article’;
};
?>
And voila, just like that every single post is now an Exclusive article!
More examples
Here are some real world examples you can play with and use:
https://wpmudev.com/blog/making-conditional-tags-work-magic-in-your-wordpress-site/