WordPress Conditional Tags and the Widget Logic plugin for WordPress

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?

  1. is_home()
  2. is_front_page()
  3. is_admin()
  4. is_single()
  5. is_sticky()
  6. is_post_type_hierarchical( $post_type )
  7. is_post_type_archive()
  8. is_comments_popup()
  9. comments_open()
  10. pings_open()
  11. is_page()
  12. is_page_template()
  13. is_category()
  14. in_category( $category_id )
  15. is_tag()
  16. has_tag()
  17. is_tax()
  18. is_archive()
  19. taxonomy_exists()
  20. is_author()
  21. is_multi_author( )
  22. is_date()
  23. is_year()
  24. is_month()
  25. is_day()
  26. is_time()
  27. is_new_day()
  28. is_search()
  29. is_404()
  30. is_paged()
  31. is_attachment()
  32. is_singular()
  33. is_feed()
  34. is_trackback()
  35. is_preview()
  36. has_excerpt()
  37. has_nav_menu()
  38. in_the_loop()
  39. is_active_sidebar()
  40. is_multisite()
  41. is_super_admin()
  42. is_plugin_active()
  43. 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.

  1. Use conditional tags to display a custom breadcrumb.
  2. Use it on single entry/article pages to show a custom advertisement box just below the post.
  3. Add Facebook comments to your single entry pages.
  4. 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.

Widgets ‹ IncrediBlogger — WordPress WordPress Conditional Tags and the Widget Logic plugin for WordPress

Article by Prasenjit Haty

My interests and hobbies are all centered around my Blog. Research on Affiliate Marketing and Online Businesses and always trying to explore new ways for it are a few of my favorite pastimes. You can connect with me on: Google+ / Facebook / Twitter


Prasenjit has written 74 awesome articles for us.

Comments

  1. 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.

  2. Nice collection!
    I’ve tried to figure out how use widget logic with negative conditionals
    !is_tag("thetag") doesn’t work. Any clues?

Leave a Reply

%d bloggers like this: