Rarely Used but Useful WordPress Conditional Tags

The conditional tags in WordPress take a lot of burden from our shoulders. The core of this concept is you get a bunch of simple functions which return “true” or “false” boolean value based on the environment. When you develop, these features can be convenient because you can check a lot of states which a simple line.

These bits are necessary for theme development, and there are a lot of them which you can find in the handbook. I thought that I collect some rarely used (of course by me) and write a few line about is. If you are an intermediate developer I’m sure that you use these so this article is not for you! 😉

is_singular()

I often use the is_single() and is_page() method if I like to check my template in the header, the footer or the functions.php. The is_singular() is combine the mentioned two with a third named is_attachment(). If you want to target all of your single like templates you can use this.

By default, it is run on all post type, but if you want a narrow selection, you want to give a single string parameter or an array of multiple values.

if ( is_singular() ) {}
if ( is_singular( 'book' ) ) {}
if ( is_singular( array( 'post', 'book' ) ) ) {}

is_admin_bar_showing()

The is_admin_bar_showing() function gives back a “true” value when the admin bar is showing (for this we must be logged in).

The admin bar is always visible in the administration section of the page, but we can turn off it on the front-end; for this, you can use this filter: add_filter(‘show_admin_bar’, ‘__return_false’); .

The method can be used if you want to target this part of your site. For example, if you’re going to remove the fixed positioning of this area when browsing the front-end like the following:

function admin_bar_position_method() {
   if ( is_admin_bar_showing() ) {
       $custom_css = "
           #wpadminbar {
               position: absolute;
               margin-top: -46px;
           }

           @media (min-width: 782px) {
               #wpadminbar {
                   margin-top: -32px;
               }
           }
       ";
       wp_add_inline_style( 'my-theme-style', $custom_css );
   }
}

add_action( 'wp_enqueue_scripts', 'admin_bar_position_method' );

Of course you also place this code in your CSS but this is the WordPress way and in this case you add this code when it needed. Another interesting bit here is the wp_add_inline_style() using this function you can add inline CSS after one of your enqueued stylesheet.

is_template_page()

Whit the is_template_page() function you can check a specific WordPress page template; for the checking, you have to pass the template file name to it.

For me using template pages comes handy when I make a multilanguage site and I don’t want to duplicate the page templates, but you can also use this feature for any custom part. In this case there a lot of use cases where you like to target these templates for me this mostly happens incase different header variants.

if ( is_page_template( 'about.php' ) ) {}

has_tag(), has_term()

These two can be useful when you want to make some specific styling based on tag or term.

Let’s suppose you want to highlight some part of your template. For this, you should add the tag/term name in a string for a singular value or in an array in case of multiple values. Using has_term() you also have to set up a second parameter which is the taxonomy slug.

if ( has_term( array( 'green', 'orange', 'blue' ), 'color' ) ) {}
if ( has_tag( array( 'sharp', 'mild', 'extreme' ) ) ) {}

is_preview()

The is_preview() method gives back a “true” value when you use the admin’s preview feature.

If you use Disqus comment system, you see that in preview mode it is unavailable. With this conditional tag, you can achieve the same effect with any part of your single files. It can be a good idea to make a simple infobox for the user that he/she is watching just a preview and this is the behavaior for some different appearance.

if ( is_preview() ) {}