Introduction To WordPress Conditional Tags

WordPress conditional tags are one of the great features in WordPress. It is fundamental in the development of WordPress themes and plugins. Knowledge about WordPress conditional tags not only crucial for developers but also good for end users as well.

Therefore, in this article, I would like to share about WordPress conditional tags and how to use these conditional tags.

About WordPress Conditional Tags

What Is WordPress Conditional Tags

WordPress conditional tags are a set of boolean data type used to check whether specific conditions are met before any codes are allowed to be executed. With WordPress conditional tags, you can control when, where or which of your codes needs to be implemented.

For example, you can set to run code A only if your visitor visits an attachment post type. Or, run code B if the logged-in user has a specific role like admin, author or subscriber.

Why You Should Use WordPress Conditional Tags

In case you didn't know, most hosting providers have set execution limit for their hosting package. Where each time someone visits your website and a script executed, it will count as one execution.

So, more script you have and more visitors load your website will make you reach your execution limit faster. And once you reach this limit, your website will be no longer accessible.

This can be decreased by using WordPress conditional tags. In which will allow your script to be executed only on the page it needs to and not on every page. And at the same time not only reduce stress on your server but also prevent from wasting unnecessary server resources.

Side note: Besides using WordPress conditional tags, you can also drastically reduce your script execution by using Memcached.

Common WordPress Conditional Tags

Currently, there are a total of 61 WordPress conditional tags. However, below is the 10 most common tags used by developers. And a complete list of conditional tags will be shared in further reading.

Conditional TagsOptional ParametersNotes
is_single()Integer, string and array.For single posts and custom post type.
is_page()Integer, string and array.For pages.
is_front_page()For website front page. See note 1.
is_home()For website home page. See note 2.
is_singular()String and array.For single posts, custom post type, pages and attachment pages. See note 3.
is_category()Integer, string and array.For category archives.
is_tag()Integer, string and array.For tag archives.
is_404()For 404 page.
is_search()For search result page.
is_author()Integer, string and array.For author pages.

Notes:

  1. The returned value depends on your settings in Settings » Reading » Your homepage displays.
    • Your latest posts will return as TRUE.
    • A static page (homepage) will return as TRUE.
    • A static page (blog post) will return as FALSE.
  2. The returned value depends on your settings in Settings » Reading » Your homepage displays.
    • Your latest posts will return as TRUE.
    • A static page (homepage) will return as FALSE.
    • A static page (blog post) will return as TRUE.
  3. This might be a little bit confusing for beginners. Because it will return TRUE on all single posts, custom post type, pages, and attachment pages. Usually, it is used to check custom post type.

How To Use WordPress Conditional Tags

The use of WordPress conditional tags is pretty straightforward. Some tags accept integer, string or array as optional parameters where you can achieve even more specific result.

Let's take is_single() tag as an example. This tag accepts single post ID, title and slug as a parameter. Additionally, it also accepts an array if you want to assign multiple values. Please take a look at the snippet below.

How To Use Multiple Conditional Tags

Using ELSE Or ELSEIF

In the above examples, we have used one conditional tag in the conditional statement. But sometimes you may have more conditions that need to be set in order to get the results you want. This can be achieved by adding ELSE or ELSEIF in the conditional statement. Please see the snippet below.

The first example above is self-explanatory. We generally tell WordPress to run code A on all single post and custom post type but run code B elsewhere.

While the second example seems a little complicated but actually it's not. In simple word, we specifically tell WordPress to run code A only on a single post with ID 7 and run code B only on a single post with title "Fruit".

And at the same time, only run code C on the page type and run code D on other parts of the website.

Using Logical Operators

Additionally, you can also use logical operators to assign more conditions. There are 6 PHP logical operators you can use in your conditional statement. However, please keep in mind that there are two different variations of AND and OR operators which operate at different precedences.

The first example above which using OR operator is pretty obvious. We basically tell WordPress to run code A if visitors visit any of single post, page or custom post type. Otherwise, run code B on another part of the website.

AND operator, on the other hand, requires all conditions to be met before your code is allowed to run. In the second example, your code will only run if your visitors have logged in and are currently viewing any single post or custom post type.

Sometimes, you may want to run your script everywhere but one place. You can do this by using NOT operator. For instance, you can instruct WordPress to run your code A everywhere except in single post and custom post type. Please see the third example above.

More WordPress Conditional Tags

As I have stated above, there are currently 61 WordPress conditional tags available you can use. However, new tags may be introduced or existing tags may be removed from time to time during WordPress updates. And to see the complete list of current conditional tags, please visit the official WordPress website.

Footer Note

In brief, WordPress conditional tags is a great way to reduce script execution for websites. Not only it is easy to use but the implementation of these tags will also significantly reduce stress on the server. And for beginner developers, I hope this article will give you a basic understanding of how to use WordPress conditional tags.

  • If you think your friends would find this useful, please share it with them. It will allow us help more people.
  • To get more of our latest update, please subcribe to our newsletter.
  • You can also print this article for reference.
This article has been printed from https://www.jebatonline.com/articles/introduction-to-wordpress-conditional-tags/

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.