How to Use Conditional Tags in WordPress

Published in WordPress.

Watch our video

There is a better version of your web

Share this post

One of the things I like most about WordPress is that it includes a lot of functions that greatly simplify our work as developers. For example, consider functions like esc_attr, which escapes text so that it can be safely added in an HTML tag attribute, or esc_html, which escapes text so that it can be safely included in an HTML page. Or take a look at some of the functions we have to sanitize the data we receive from a user: sanitize_text_field, sanitize_textarea_field or sanitize_email. You get so used to these well-defined functions that you really miss them when you have to develop anything outside the WordPress realm and discover they’re no longer available!

The collection of functions we have at our disposal when we develop on WordPress is incredibly large. And the more familiar we are with them, the easier it is to write high quality code.

Today I would like to tell you about conditional tags in WordPress: what they are, how to use them, what can be misleading about them, and why they are so useful. If you are a plugin or theme developer and you don’t know them yet, don’t miss this post.

What Are WordPress’ Conditional Tags?

Conditional tags are functions defined in WordPress whose result is a boolean value (that is, it’s either true or false). These are functions we can use both in our plugins and themes to make conditional decisions based on the content we’re about to render.

What makes conditional tags great is that, in general, their names are self-explanatory and result in code that’s very easy to understand. For example, if you use the conditional tag is_sticky in your plugin, it will clearly return true if, when inside the WordPress loop, we’re processing a sticky post. No doubt about its semantics, right?

Before we get started with a few examples, please keep in mind that conditional tags are only available once WordPress has determined what content the visitor has requested. If we try to use them before, their result will always be false. In other words, we can only use them once the posts_selection action has run.

Most Common Conditional Tags

Most conditional tags in WordPress start with is_, since the answer to a question like “is blah blah?” is a boolean value: either “yes” or “no”. But, unfortunately, this criterion is not always followed (as we will see below).

There are many conditional tags within WordPress that you will become familiar with as you need them. But there are quite a few that are extremely common and the sooner you get to know them, the better. So let’s take a closer look at those:

  • is_single. It checks whether the current element is a WordPress post or an instance of any other custom post type (such as, for example, a WooCommerce product).
  • is_page. It checks whether the current element is a WordPress page.
  • is_attachment. It checks whether t he current element is an attachment. Attachments are those files that you upload to your Media Library, such as images, videos, files, etc.
  • is_singular. It checks whether any of the three previous functions is true. That is, it checks if the current element is a post, page, attachment, or any other post type.
  • comments_open. It returns true if visitors can comment on the current element. See? Here you have an example where the conditional tag doesn’t start with is_.
  • is_category. It checks if we’re currently looking at a category archive page.
  • in_category. It returns true if the current post is in the given category.
  • is_tag. It checks if we’re currently looking at a tag archive page.
  • has_tag. It checks if the current post is tagged using the specified tag.
  • is_main_query. It checks if we’re in WordPress’ main loop.

Did you know that there are conditional tags that can be misleading?

There are some cases where conditional tags have names that can lead us to confusion since what they seem to evaluate and what they actually evaluate are different things:

  • is_admin. You might think that this function checks if the current user is an administrator, right? Wrong! It checks whether we are in a page within the WordPress Dashboard (and then it’s result would be true) or in the front-end (that is, false).
  • is_home. I used to think that this function checks if the current page is your home page. For example, in our website, it’d be https://neliosoftware.com. But unfortunately, that’s not the case. WordPress was born as a blogging platform and I think this is an example of some “legacy code.” is_home is a conditional function that checks if the current page is your Blog page. Thus, for example, if you’re using WordPress’ default setup, is_home does indeed return true when a visitor is in the home page (https://yourweb.com), because that page shows your latest blog posts. But if you set your blog page to something else (like https://yourweb.com/blog), then is_home will be true when the visitor requests https://yourweb.com/blog and false otherwise.
  • is_front_page. And this is the conditional tag that’ll let you know when the visitor is in the “home page.”
  • is_dynamic_sidebar. This function checks if your theme has one or more widget sidebars and, if it does, it’ll return true if at least one of those sidebars is not empty.

Parameterizing Conditional Tags

Although most conditional tags are functions that can be called without parameters, many of them support parameters. If we use them, we can be more specific about the criteria the current post should meet. And they do this in quite a logical way.

Depending on the specific function we are using, we might want to check if the current element has a certain ID or a certain slug, if it’s an instance of a certain post type, or even if it has a specific title. For instance:

  • is_singular( 'product' ) checks if the current element is a product.
  • is_page( 'Contact Us' ) checks if the current page’s title is “Contact Us.” This function accepts different types of arguments and, depending on what you provide, it’ll check one thing or the other. For instance, you can check if the current element has a certain ID or a certain title or slug. Pretty neat, huh? You can even use lists as parameters!
  • is_single( [ 5, 10, 11 ] ) checks if the current element’s ID is either 5, 10, or 11. It’s quite similar to is_page, but, as we said before, it works for posts other than page.

How to Use Conditional Tags

Let’s take a look at a couple of examples where conditional tags might come handy.

Customizing Templates

As you probably foresaw, conditional tags are obviously useful in themes. If you want the theme to show one information or another depending on the type of content being viewed at any given time (and this is something we will want to do often), conditional tags are your allies:

<?php
// ...
if ( ! is_page() ) {
  echo '<span class="whatever">';
  echo $something_relevant;
  echo '</span>';
}
// ...

This allows you to use the same template for different types of content and customize each of them with conditional tags.

Conditionally Enqueuing Assets

But conditional tags are extremely useful to plugin developers too. Specifically, they help us load parts of our plugin conditionally, depending on the results we get from conditional tags.

For example, imagine we are developing a plugin that adds some kind of functionality to our blog posts. Suppose this functionality requires that we enqueue a script and a stylesheet to the front-end. To enqueue scripts and styles in WordPress we have a filter called wp_enqueue_scripts:

function nelio_plugin_fancy_feature() {
  wp_enqueue_script( 
    'nelio_plugin_feat', 
    nelio_plugin()->url . '/assets/dist/feat.js'
  );
  wp_enqueue_style( 
    'nelio_plugin_feat', 
    nelio_plugin()->url . '/assets/dist/feat.css'
  );
}
add_action( 'wp_enqueue_scripts', 'nelio_plugin_fancy_feature' );

The problem with the previous snippet is that it enqueues our assets always, no matter what the visitor requested. Whenever the wp_enqueue_scripts runs (and it does in every front-end request), our callback function will be called and our script and stylesheet will be added. But we said our feature only affects blog posts so… do we really need to enqueue these assets always? Of course not! And we can improve this by using the appropriate conditional tag:

function nelio_plugin_fancy_feature() {
  if ( ! is_single( 'post' ) ) {
    return;
  }

  wp_enqueue_script( 
    'nelio_plugin_feat', 
    nelio_plugin()->url . '/assets/dist/feat.js'
  );
  wp_enqueue_style( 
    'nelio_plugin_feat', 
    nelio_plugin()->url . '/assets/dist/feat.css'
  );
}
add_action( 'wp_enqueue_scripts', 'nelio_plugin_fancy_feature' );

In Summary

WordPress’ Conditional Tags let us test different criteria of our content and thus adapt what will happen based on that. Despite their extreme simplicity, they are very useful functions that make our code more intelligible and efficient.

I hope you learned something today and, as always, if you have any questions or comments, please let us know in the comment section below.

Featured image by Justin Luebke on Unsplash.

Leave a Reply

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

I have read and agree to the Nelio Software Privacy Policy

Your personal data will be located on SiteGround and will be treated by Nelio Software with the sole purpose of publishing this comment here. The legitimation is carried out through your express consent. Contact us to access, rectify, limit, or delete your data.