All those wordpress theme developers and designers are very well versed with wordpress conditional tags. In this article I’ll explain what conditional tags are and how you can use them to design your custom theme.
What are Conditional Tags?
WordPress conditional tags are nothing but a set of handy inbuilt functions which return either ‘TRUE’ or ‘FALSE’. So technically speaking, conditional tags are functions whose return type is Boolean.
Why Conditional Tags?
Let’s say, you want to show a specific footer widget only on your blog home page or you want to list a set of related articles on a single entry page. This is when wordpress conditional tags can come in handy. By using these conditional tags in your theme, you can easily manipulate the behavior and look of your theme.
Which Conditional Tags are available to Theme Developers?
- is_home()
- is_front_page()
- is_admin()
- is_single()
- is_sticky()
- is_post_type_hierarchical( $post_type )
- is_post_type_archive()
- is_comments_popup()
- comments_open()
- pings_open()
- is_page()
- is_page_template()
- is_category()
- in_category( $category_id )
- is_tag()
- has_tag()
- is_tax()
- is_archive()
- taxonomy_exists()
- is_author()
- is_multi_author( )
- is_date()
- is_year()
- is_month()
- is_day()
- is_time()
- is_new_day()
- is_search()
- is_404()
- is_paged()
- is_attachment()
- is_singular()
- is_feed()
- is_trackback()
- is_preview()
- has_excerpt()
- has_nav_menu()
- in_the_loop()
- is_active_sidebar()
- is_multisite()
- is_super_admin()
- is_plugin_active()
- current_theme_supports()
Well not all these tags are used very frequently. All the function names are self-explanatory and you can easily understand from the name where to fit in the tag into your theme.
A complete list of Conditional Tags is provided on the WordPress Codex.
How to use WordPress conditional tags
If you know PHP or at least have some exposure to any programming language, then this part is going to be a breeze. Basically you need to know how to make use of the if condition. Since all the above functions return you either true or false, hence they should be used inside an ifcondition(dummy code below).
Code:
if (condition_is_met) {
Perform a set of operations.
}
Example 1: Display a Welcome Message to your visitors while on the Homepage.
Live Code:
<?php if(is_home()) { ?>
<p>Welcome visitor, perhaps you would like to subscribe to my feed? This message will only be displayed on the homepage.</p>
<?php } ?>
Example 2: Display an author box on Single Entry Pages
Live Code:
<?php if(is_single()) { ?>
<div id="authorbox">
<?php echo get_avatar( get_the_author_meta('user_email') , 100 ); ?>
<h4>Article by <a href="<?php $author_link = get_author_posts_url($post->post_author); echo $author_link; ?>" title="Posts by <?php the_author_firstname(); ?>" >
<?phpthe_author_firstname(); ?><?php the_author_lastname(); ?></a></h4>
<p class="byline"><a href="<?php the_author_meta('user_url'); ?>">Visit <?php the_author_firstname(); ?>'s blog</a></p>
<p><?php the_author_description(); ?></p>
<br/>
<p class="hlight"><?php the_author_firstname(); ?> has written <span><?php the_author_posts(); ?></span> awesome articles for us.</p>
</div>
<?php } ?>
Example 3: Show a custom comment policy above the comment form
Live Code:
<?php function custom_comment_policy() {
if(!is_user_logged_in()) { ?>
<br />Commenting Policy: <strong>Do not use just "keywords" for Name.</strong>You must leave a real name, but you can use name@keywords if you like. All comments are subject to moderation.<br /><br />
<?php }}
The above example shows how you can negate a conditional tag and use it to show a custom comment policy only to users who are not logged into your blog. So if a user is registered on your blog and is logged in, then the above message won’t be displayed to him.
Other uses of Conditional Tags
With the sample codes I’ve written above, you might have got some idea how to make use of conditional tags in your theme to manipulate the output. To help you in this aspect, here is some food for thought for you to work on and explore your creativity.
- Use conditional tags to display a custom breadcrumb.
- Use it on single entry/article pages to show a custom advertisement box just below the post.
- Add Facebook comments to your single entry pages.
- Limit the number of posts per page on your Homepage.
Where does Widget Logic fit in?
Now that you know about the different conditional tags available in WordPress and how to use them in your theme, you might be thinking “Why does the Title of this article contain the word Widget Logic”. I’ll discuss about it for the next few lines. Well, widget logic is a simple WordPress plugin which allows you to hook conditional tags for your widgets. To be precise, it allows you to show/display any widget based on conditional tags. Once installed, for each widget that you add, you get an extra text-field which allows you to enter a conditional tag making the widget visible only when the condition is met.


Very definitive guide Prasenjit, I think being an SEO wont need you to know all these tags, but if you are offering web design services, or code optimization to your clients, these tips will come in really handy, I am on your blog for the very first time, and am glad to find it.
Will be reading more.
Thanks.
Nice collection!
I’ve tried to figure out how use widget logic with negative conditionals
!is_tag("thetag")doesn’t work. Any clues?Hi Nikke,
Try
!is_tag('the_tag_slug'). Instead of the tag itself, you should pass the tag slug as an argument to the function and it should work.Hope you have a wonderful day.
Thanks.
Many thanks!
I ended up using this kind of statement, since I don’t want it on every other page on the site:
is_home() || is_tag() && !is_tag(“the-slug”)