A Walkthrough on Conditional Tags in WordPress: 1 to 13

In the first part of this series, we went through the fundamentals of Conditional Tags—what they are, how to use them, and some scenarios where Conditional Tags come in handy.

In this second part, we're going to start reviewing 13 Conditional Tags, and in five articles (including this one), we'll finish going through all 65 Conditional Tags that are documented in the Codex. Be sure to check out the first part if you haven't yet.

Let's begin!

1. Checking Whether We're in the "Blog Posts Index Page": is_home()

This poorly named Conditional Tag checks whether the blog posts index page is being displayed. In the old times, when WordPress was just a "blogging platform", the "home page" meant the list of your latest blog posts. But after WordPress grew to be a content management system, the name is_home() became kind of obsolete.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_home()

Let's say you want to greet your visitors on the homepage. Here's what you do:

2. Checking Whether the Current Theme Is a Child Theme: is_child_theme()

While developing with WordPress, you may need to check whether a child theme is in use. If that's the case, the Conditional Tag is_child_theme() will help you by returning TRUE or FALSE when a child theme is being used or not.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

3. Checking Whether the Post Is in the Given Category: in_category()

In a WordPress project, you might want posts from different categories to behave differently. For example, you might want to add classes to certain posts, or hide those posts from the general post listings altogether. The Conditional Tag in_category() allows you to determine posts published in a certain category or categories.

Accepted Parameters

This Conditional Tag has two parameters:

  • $category (array/string, required): Category ID, name, slug or an array of those. (Default: None)
  • $post (object/integer, optional): The post (ID or object) to check. (Default: Current post)

Usage Example for in_category()

Let's say you have a blog with lots of categories, one being "Announcements", and you want the updates to pop out among other posts, so you want to add a custom class to the post wrapper. Here's what you do:

4. Checking Whether a "Page Template" Is in Use: is_page_template()

Page Templates is a WordPress feature that allows you to choose how certain pages will be displayed. With the help of the Conditional Tag is_page_template(), you can detect whether a certain Page Template (or any Page Template) is in use.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $template (string, optional): Template's name—with extension. (Default: None)

5. Checking Whether the Page Is an Archive Page: is_archive()

There are many kinds of archives in a WordPress website: date archives, category archives, tag archives, author archives, custom taxonomy archives... But if you want to detect archive pages in general, is_archive() is your friend: It checks whether an archive page is being displayed.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_archive()

Let's say you want to prepend post titles with some text in archive pages. Here's what you do:

6. Checking Whether the Page Is a "Date Archives" Page: is_date()

As I said before, there are many types of archive pages, and date archives are one of them. Date archives can be separated into yearly, monthly and daily archives; but if you want to detect date archive pages in general, you can use the is_date() Conditional Tag.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

7. Checking Whether the Given Widget Is in Use: is_active_widget()

Creating widgets in WordPress is easy and fun, but we might need to determine whether the widget is in use on the front end or not. The Conditional Tag is_active_widget() does exactly that: It checks whether the widget is being displayed.

Accepted Parameters

This Conditional Tag has four parameters:

  • $callback (string, optional): Widget callback to check. (Default: FALSE)
  • $widget_id (integer, optional): Widget's ID. (Default: None)
  • $id_base (string, optional): Base ID of a widget created by extending WP_Widget. (Default: None)
  • $skip_inactive (boolean, optional): Whether to skip inactive widgets or not. (Default: TRUE)

Usage Example for is_active_widget()

Let's say a widget in your theme requires jQuery to run, and you need to enqueue it conditionally. Here's what you do:

8. Checking Whether the Page Is a Single Blog Post's Page: is_single()

Want to check if the user's visiting a single post? One of the most popular Conditional Tags, is_single(), can help you. It can detect any post type, except Attachment pages and Page pages. And if you specify a post ID, post title or post slug (or an array of those), you can detect specific post(s), too.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post (array/string, optional): Post ID, title, slug or an array of those. (Default: None)

9. Checking Whether the Email Address Exists in the Users Table: email_exists()

If you need to check an email address to see whether it's in the users table of WordPress, you can use the email_exists() Conditional Tag.

This particular Conditional Tag is one of the three Conditional Tags that returns something other than TRUE—it returns the ID of the user that registered with the given email address.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $email (string, optional): Email address to check. (Default: None)

Usage Example for email_exists()

Let's say you're developing a plugin that builds an email list from the users' email addresses and lets you add and remove more email addresses with custom input, but you want to prevent deleting email addresses that belong to users. Here's what you do:

10. Checking Whether the Post Type Is Hierarchical: is_post_type_hierarchical()

Like pages and sub-pages, you can define hierarchy for new custom post types in WordPress. And the Conditional Tag is_post_type_hierarchical() lets your code know whether the given post type is hierarchical or not.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_type (string, required): name of the post type. (Default: None)

11. Checking Whether the Post Is "Sticky": is_sticky()

A "Sticky Post" is a post that stays on top of post listings, no matter when it's been published. And—I'm going to be a little discriminatory here—they deserve different treatment from other, ordinary posts. So if you want to add a post class to sticky posts, or treat them in a different way, you can make your code detect them by using the is_sticky() Conditional Tag.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_id (string, optional): The post's ID. (Default: None)

Usage Example for is_sticky()

Let's say you're making a theme and you want to display a "STICKY" ribbon in sticky posts. Here's what you do:

12. Checking Whether the Administration Panel Is Being Displayed: is_admin()

Allowing us to know whether we're in the front end or the back end, the Conditional Tag is_admin() is one of the most popular Conditional Tags among all. As the name suggests, is_admin checks whether the WordPress administration panel is being displayed.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

13. Checking Whether the Page Is a "Category Archives" Page: is_category()

When you want to detect certain category archive pages, you can use the Conditional Tag is_category(). Defining a category (or an array of categories) as its parameter, you can check whether those categories' archives are being displayed. If you leave the parameter unset, it will return TRUE when any category archive page is being displayed.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $category (string/array, optional): Category ID, title, slug or an array of those. (Default: None)

Usage Example for is_category()

Let's say you want to include a different sidebar when the "News" category archives are being displayed. Here's what you do:

Conclusion

In this part, we reviewed 13 of the 65 documented Conditional Tags in WordPress. In the next parts, we're going to go through the remaining 52. If you have any questions or comments, shoot them below—and if you liked this article, don't forget to share it!

See you in the next part!

Tags:

Comments

Related Articles