Advertisement
  1. Code
  2. WordPress
Code

A Walkthrough on Conditional Tags in WordPress: 53 to 65

by
Difficulty:BeginnerLength:MediumLanguages:
This post is part of a series called A Walkthrough on Conditional Tags in WordPress.
A Walkthrough on Conditional Tags in WordPress: 40 to 52
A Walkthrough on Conditional Tags in WordPress: Series Finale

In this series, we're going through one of the fundamental features of WordPress: Conditional Tags. In this sixth part, we'll finish introducing and reviewing the Conditional Tags. Be sure to check out the previous parts if you haven't yet.

Let's begin!

53. Checking Whether the Page Is Either a Blog Post or a Page: is_singular()

I don't know why, but this one's my favorite Conditional Tag: is_singular() can detect when any post type is being displayed. It's basically a combination of the Conditional Tags is_single(), is_attachment() and is_page(). Plus, if you set a post type (or an array of post types) as the parameter, you can detect only the post types you set.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_types (array/string, optional): Post type's name or an array of post type names. (Default: None)

54. Checking Whether the Function Is Working in "The Loop": in_the_loop()

If I say the Loop is the foundation of WordPress in terms of code, few would disagree. Thus, the Conditional Tag in_the_loop() is one of the key Conditional Tags, and it helps your functions to see whether they're inside the Loop or not.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for in_the_loop()

Let's say your plugin has a function that has to run in the Loop, but you also want to display an error in the page source code if the user uses it outside the Loop. Here's how you build the function:

55. Checking Whether the Specified Plugin Is Inactive: is_plugin_inactive()

You can guess what this Conditional Tag is about by its name: Defined as "the logical negation of is_plugin_active()" in the Codex, is_plugin_inactive() checks if the given plugin is installed, but not activated.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $plugin (string, required): Plugin's or sub-directory's name. (Default: None)

56. Checking Whether the Page Is an "Author Archives" Page: is_author()

While creating a plugin (or theme), you might want to know when an attachment page is being displayed. The Conditional Tag is_attachment() is the one that will help you: It returns TRUE in an attachment page and FALSE anywhere else.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $author (string/integer, optional): Author's username or ID. (Default: None)

Usage Example for is_author()

Let's say you're developing a Google Analytics-related plugin and you want to set a "custom variable" to detect author archives. Here's what you do:

57. Checking Whether We're on a Paged "Listing" Page: is_paged()

In blog index pages or archive pages, WordPress allows us to use pagination to divide the post listings into consecutive pages. With the help of the Conditional Tag is_paged(), you can make your code detect whether or not the listing is "paginated" and the page number is greater than 1.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

58. Checking Whether the WordPress Toolbar Is Being Displayed: is_admin_bar_showing()

The WordPress Toolbar, formerly known as the Admin Bar, is considered both useful and annoying among the WordPress community. Some love it, some hate it, so the Conditional Tag is_admin_bar_showing() could be helpful to everyone: It determines whether the Toolbar is being displayed or not.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_admin_bar_showing()

Let's say you want to extend the Toolbar with some CSS styles but you need to load the stylesheet conditionally. Here's what you do:

59. Checking Whether the Page Is a "Daily Archives" Page: is_day()

Daily archives are probably the most underused types of date-based archives, but that doesn't mean they're useless—some themes may even have different templates for daily archives. So, if you want to detect daily archive pages, you can use the Conditional Tag is_day() to do the job.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

60. Checking Whether the Given Sidebar Is in Use: is_active_sidebar()

While creating a theme, we must take those who don't use any widgets into account. The Conditional Tag is_active_sidebar() checks whether the given sidebar has any widgets in it.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $sidebar (string/integer, required): Sidebar's name or ID. (Default: None)

Usage Example for is_active_sidebar()

This example comes from the Twenty Ten theme—it uses the Conditional Tag to conditionally load the given sidebar:

61. Checking Whether the Given Username Exists in the Users Table: username_exists()

The third Conditional Tag that returns something other than TRUE when successful is username_exists(). This Conditional Tag checks the given username and returns the ID of the user if a user with the given username exists. If it doesn't, it returns NULL.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $username (string, required): The username to check. (Default: None)

62. Checking Whether It's the "Preview Post" Page: is_preview()

If you have some page elements that shouldn't be loaded in previews (like impression-based ads or some sensitive analytics code), you can remove them with the help of the Conditional Tag is_preview().

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_preview()

It's good practice to hide preview pages from Google Analytics, so you need to be careful and include your Google Analytics script like this:

63. Checking the State of the Given Script: wp_script_is()

This is particularly useful for plugin developers who don't want their scripts to conflict with other enqueued scripts. With this Conditional Tag, you can detect whether the given script is registered, enqueued, enqueued and printed, or enqueued but not printed yet.

Accepted Parameters

This Conditional Tag has two parameters:

  • $handle (string, required): Name of the script (in lowercase). (Default: None)
  • $list (string, optional): One of the four strings—"registered", "enqueued", "done" (enqueued and printed), or "to_do" (enqueued but not printed). (Default: "enqueued")

64. Checking the State of the Given Style: wp_style_is()

This Conditional Tag is similar to its brother, wp_script_is(), but it does the same job for enqueued stylesheets. With this Conditional Tag, you can detect whether the given style is registered, enqueued, enqueued and printed, or enqueued but not printed yet.

Accepted Parameters

This Conditional Tag has two parameters:

  • $handle (string, required): Name of the style (in lowercase). (Default: None)
  • $list (string, optional): One of the four strings—"registered", "enqueued", "done" (enqueued and printed), or "to_do" (enqueued but not printed). (Default: "enqueued")

Usage Example for wp_style_is()

Let's say you're making a theme with the Bootstrap framework and you created a custom Bootstrap theme to include. You need to ensure Bootstrap's main CSS files are enqueued first:

65. Checking Whether the Taxonomy Is Hierarchical: is_taxonomy_hierarchical()

You know categories are hierarchical—you can set parent and child categories. The same goes for custom taxonomies, if you make it so while creating it. And if you want your code to know whether a certain taxonomy is hierarchical, you can use the is_taxonomy_hierarchical() Conditional Tag.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $taxonomy (string, required): Taxonomy name. (Default: None)

Conclusion

In this part, we went through the last batch of the 65 documented Conditional Tags in WordPress. In the next (and last) part of the series, we'll finish off by reviewing what we learned and a tiny bonus part that—hopefully—you'll enjoy.

If you have any questions or comments, shoot them below—and if you liked this article, don't forget to share it!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.