A Walkthrough on Conditional Tags in WordPress: Series Finale

It was a long run, but we finally got to the last part of the series. I hope it was a fun and educational journey and you enjoyed the series as much as I did.

What We Learned

The first part was about introducing Conditional Tags. In this part, we learned about what Conditional Tags are, how important they are for WordPress theme and plugin developers, and how to use Conditional Tags in WordPress development. In addition, I came up with five different example cases in which Conditional Tags come in handy.

In the following five tutorials, we went through 65 Conditional Tags in total. There were 66 documented Conditional Tags when I started and finished writing this series. One of them was a deprecated function (is_plugin_page()), so I wrote about 65 Conditional Tags, with descriptions, parameters and examples for some of them.

In the second part, we learned about the following Conditional Tags:

  • Checking whether we're on the "blog posts index page": is_home()
  • Checking whether the current theme is a child theme: is_child_theme()
  • Checking whether the post is in the given category: in_category()
  • Checking whether a "page template" is in use: is_page_template()
  • Checking whether the page is an archive page: is_archive()
  • Checking whether the page is a "date archives" page: is_date()
  • Checking whether the given widget is in use: is_active_widget()
  • Checking whether the page is a single blog post's page: is_single()
  • Checking whether the email address exists in the Users table: email_exists()
  • Checking whether the post type is hierarchical: is_post_type_hierarchical()
  • Checking whether the post is "sticky": is_sticky()
  • Checking whether the administration panel is being displayed: is_admin()
  • Checking whether the page is a "category archives" page: is_category()

In the third part, we went through these Conditional Tags:

  • Checking whether we're on the front page: is_front_page()
  • Checking whether the post has a thumbnail: has_post_thumbnail()
  • Checking whether the theme is using the "comments popup": is_comments_popup()
  • Checking whether the page is a 404 error page: is_404()
  • Checking whether the given taxonomy exists: taxonomy_exists()
  • Checking whether the page is the "search results" page: is_search()
  • Checking whether the page is a "tag archives" page: is_tag()
  • Checking whether the post has a custom excerpt: has_excerpt()
  • Checking whether it's the main query: is_main_query()
  • Checking whether the post has the given tag: has_tag()
  • Checking whether the blog is installed: is_blog_installed()
  • Checking whether the user is a "super admin": is_super_admin()
  • Checking whether the page is a "page" page: is_page()

In the fourth part, we checked out the following:

  • Checking whether the page is a "monthly archives" page: is_month()
  • Checking the current theme's features: current_theme_supports()
  • Checking whether the specified plugin is active: is_plugin_active()
  • Checking whether the URL is a local attachment: is_local_attachment()
  • Checking whether the page is a time-based archive page: is_time()
  • Checking whether the current locale is RTL: is_rtl()
  • Checking whether the page is a custom taxonomy's "archives" page: is_tax()
  • Checking whether the page is an attachment page: is_attachment()
  • Checking whether the given term exists: term_exists()
  • Checking whether the post has the given term: has_term()
  • Checking whether it's a trackback: is_trackback()
  • Checking whether "WordPress Multisite" is in use: is_multisite()
  • Checking whether the page is a post type(s) archive: is_post_type_archive()

In the fifth part, we examined these ones:

  • Checking whether the blog is the "main site" of the network: is_main_site()
  • Checking whether a menu location has an assigned menu: has_nav_menu()
  • Checking whether the specified plugin is active in multisite: is_plugin_active_for_network()
  • Checking whether comments are enabled: comments_open()
  • Checking whether a sidebar contains any widgets: is_dynamic_sidebar()
  • Checking whether there's more than one author in the blog: is_multi_author()
  • Checking whether pings are open: pings_open()
  • Checking whether a feed is being displayed: is_feed()
  • Checking whether the page is a "yearly archives" page: is_year()
  • Checking whether the visitor is a logged-in user: is_user_logged_in()
  • Checking whether the attachment is an image: wp_attachment_is_image()
  • Checking whether the given post type exists: post_type_exists()
  • Checking whether the current post is published on a new day: is_new_day()

And in the sixth part, we studied the following Conditional Tags:

  • Checking whether the page is either a blog post or a page: is_singular()
  • Checking whether the function is working in "the Loop": in_the_loop()
  • Checking whether the specified plugin is inactive: is_plugin_inactive()
  • Checking whether the page is an "author archives" page: is_author()
  • Checking whether we're on a paged "listing" page: is_paged()
  • Checking whether the WordPress Toolbar is being displayed: is_admin_bar_showing()
  • Checking whether the page is a "daily archives" page: is_day()
  • Checking whether the given sidebar is in use: is_active_sidebar()
  • Checking whether the given username exists in the Users table: username_exists()
  • Checking whether it's the "preview post" page: is_preview()
  • Checking the state of the given script: wp_script_is()
  • Checking the state of the given style: wp_style_is()
  • Checking whether the taxonomy is hierarchical: is_taxonomy_hierarchical()

Tiny Bonus Chapter: Three Plugins Making Use of Conditional Tags

In this "bonus chapter", we're going to look at three plugins that focus on using Conditional Tags. These plugins can be very, very powerful when you need them and if you use them right!

Widget Logic

Widget Logic

Downloaded nearly a million times and with a star rating of 4.3, Widget Logic is by far the most popular plugin that makes use of Conditional Tags. And it's not surprising that it's so popular, because it virtually keeps us from having to create separate sidebars for separate occasions. 

The logic of this plugin is simple: It places a little input under each widget in the Widgets page of your admin panel, so that you can type in Conditional Tags, just like when you write in your if statements. Be careful though: This plugin uses EVAL functions, which means that anyone who has access to Widget Logic inputs can write any kind of PHP code. Any kind. Be warned.

Script Logic

Script Logic

Just as Widget Logic handles widgets, Script Logic handles your JavaScript files—as long as you enqueue them with the wp_enqueue_script() function. It basically takes all the enqueued scripts and lets you add Conditional Tags to wrap the scripts and load them conditionally. This script also uses EVAL functions to work, so be careful.

Conditional Shortcodes

Remember the example we looked at for the is_feed() Conditional Tag? This plugin extends it to 19 Conditional Tags in total, including our example. Here's the list of Conditional Tags the plugin supports:

  • comments_open()
  • is_archive()
  • is_author()
  • is_category()
  • is_day()
  • is_feed()
  • is_front_page()
  • is_home()
  • is_month()
  • is_page()
  • is_page()
  • is_search()
  • is_single()
  • is_singular()
  • is_sticky()
  • is_tag()
  • is_tax()
  • is_time()
  • is_year()

The usage is just like any other shortcode: Put the Conditional Tag in square brackets and pass the parameters of the Conditional Tag as shortcode parameters. The plugin also has "else" shortcodes that you can use inside the listed shortcodes.

The End

As I said, this series was a long but fun journey for me. And I hoped you enjoyed it as much as I did.

If you have any questions, contributions or comments, shoot them below in the comments. And if you liked the series, don't forget to share it with your friends!



Related Articles