A Walkthrough on Conditional Tags in WordPress: 40 to 52

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

Let's begin!

40. Checking Whether the Blog Is the "Main Site" of the Network: is_main_site()

If you're developing for WordPress Multisite, eventually there will come a time when you need to detect the main site. The Conditional Tag is_main_site will help you then: It determines whether the given site ID is the main site of the network.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $site_id (integer, optional): The site's ID to check. (Default: Current site ID)

41. Checking Whether a Menu Location Has an Assigned Menu: has_nav_menu()

While creating a custom navigation menu, you need to specify a "menu location" with the two parameters of the register_nav_menu(s) function(s). The Conditional Tag has_nav_menu() checks whether there's a custom menu the user assigned to the given location.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $location (string, optional): Slug of the menu location. (Default: None)

Usage Example for has_nav_menu()

Let's say one of the custom menu locations of your theme needs a little JavaScript file to work properly, so you want to enqueue the script only if the menu is being used by the user. Here's what you do:

42. Checking Whether the Specified Plugin Is Active in Multisite: is_plugin_active_for_network()

Similar to is_plugin_active(), the Conditional Tag is_plugin_active_for_network() will detect whether the given plugin is active... in a Multisite installation. This might be useful if your code needs to know whether another plugin is active through the whole network, rather than a single site.

Accepted Parameters

This Conditional Tag has only one parameter:

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

43. Checking Whether Comments Are Enabled: comments_open()

One of the most commonly used Conditional Tags is comments_open(). With this function in your if statement, you can determine whether comments are enabled in the current post.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_id (integer, optional): The post ID. (Default: 0)

Usage Example for comments_open()

Let's say you want to echo a warning before the comments section, if comments are enabled for the post. Here's what you do:

44. Checking Whether a Sidebar Contains Any Widgets: is_dynamic_sidebar()

Many WordPress themes use sidebars to display widget content. But if you're developing a plugin or a theme and want to determine unused sidebars, you can use the Conditional Tag is_dynamic_sidebar()—it checks whether a sidebar is active and has any widgets used in it.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

45. Checking Whether There's More than One Author in the Blog: is_multi_author()

Most WordPress websites, I think, run with only one user. Corporate websites usually don't need more than one user, and the internet is filled with "personal blogs" (which is a good thing, don't get me wrong). However, you might want to check whether more than one author has published posts. If that's the case, is_multi_author() can help you detect WordPress installations with multiple authors.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_multi_author()

Let's say you're making a plugin just for blogs with multiple authors, and you want to warn single authors that the plugin won't work for them. Here's what you do:

46. Checking Whether Pings Are Open: pings_open()

If you still use trackbacks for some reason (or you need your plugin to work on really old blogs), you can determine whether trackbacks and pings are enabled in a specific post (or the post being displayed) with the help of the pings_open() Conditional Tag.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_id (integer, optional): The post ID. (Default: 0)

47. Checking Whether a Feed Is Being Displayed: is_feed()

I still love feeds, but they are an obsolete part of the web nowadays. And WordPress uses them, too: It supports four different kinds of feeds in its core. If you want your function to know when it's running in a feed, you can use the is_feed() Conditional Tag—it checks whether the current query is for a feed.

Accepted Parameters

This Conditional Tag has only one parameter:

  • $feeds (string/array, optional): Feed types. (Default: None)

Usage Example for is_feed()

Let's say you want to publish extra content in each post for your feeds (to encourage more people to subscribe). You will need a shortcode. Here's what you do:

48. Checking Whether the Page Is a "Yearly Archives" Page: is_year()

In blogs that you don't write frequently, it might be a better idea to promote yearly archives rather than monthly archives. And if you want to add different functionality or display a different design, you can use is_year() to detect year-based archive pages.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

49. Checking Whether the Visitor Is a Logged-in User is_user_logged_in()

It's a common thing to run different code for logged-in users: It might be a new menu item, it might be an extra comment field, or it might be a completely different website design. Whatever the case may be, you can detect logged-in users with the is_user_logged_in() Conditional Tag.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Usage Example for is_user_logged_in()

Let's say you want to greet your users differently than visitors. Here's what you do:

50. Checking Whether the Attachment Is an Image: wp_attachment_is_image()

This Conditional Tag's job is very simple: You pass the post ID as the parameter, and the Conditional Tag returns TRUE if the post's attachment is a JPG, JPEG, GIF or PNG file (and FALSE if it's not).

Accepted Parameters

This Conditional Tag has only one parameter:

  • $post_id (integer, virtually required, technically optional): The post ID. (Default: 0)

Why "virtually required" and "technically optional"? Because it defaults to 0, meaning that if you use this Conditional Tag without its parameter, it will try to return a post that doesn't exist.

51. Checking Whether the Given Post Type Exists: post_type_exists()

In some scenarios, it might be a good idea to check whether a certain custom post type is already in use. (If you're creating a portfolio plugin, for example, you might want to try to duplicate posts from a commonly used post type name for portfolios.) To do that, you can use the post_type_exists() Conditional Tag.

Accepted Parameters

This Conditional Tag has only one parameter:

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

Usage Example for post_type_exists()

Let's say you're developing a "portfolio" plugin and you chose the custom post type name "portfolio" (naturally). But many developers use the same name for portfolio post types, so you need to warn the user if another plugin or theme has already registered the post type:

52. Checking Whether the Current Post Is Published on a New Day: is_new_day()

Some WordPress functions solve the tiniest issues, and is_new_day() is one of them: This particular Conditional Tag returns TRUE if the current post's day is different than the previous one.

Accepted Parameters

This Conditional Tag doesn't accept any parameters.

Conclusion

In this part, we reviewed another batch of the 65 documented Conditional Tags in WordPress. In the next part, we're going to go through the remaining 13. 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