Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 12, 2015 12:00 pm

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()

  • Checkingwhetherthe current theme is a child theme: is_child_theme()

  • Checkingwhetherthe post is in the given category: in_category()

  • Checkingwhethera "page template" is in use: is_page_template()

  • Checkingwhetherthe page is an archive page: is_archive()

  • Checkingwhetherthe page is a "date archives" page:is_date()

  • Checkingwhetherthe given widget is in use: is_active_widget()

  • Checkingwhetherthe page is a single blog post's page: is_single()

  • Checkingwhetherthe email address exists in the Users table: email_exists()

  • Checkingwhetherthe post type is hierarchical: is_post_type_hierarchical()

  • Checkingwhetherthe post is "sticky":is_sticky()

  • Checkingwhetherthe administration panel is being displayed:is_admin()

  • Checkingwhetherthe 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()

  • Checkingwhetherthe post has a thumbnail: has_post_thumbnail()

  • Checkingwhetherthe theme is using the "comments popup": is_comments_popup()

  • Checkingwhetherthe page is a 404 error page: is_404()

  • Checkingwhetherthe given taxonomy exists:taxonomy_exists()

  • Checkingwhetherthe page is the "search results" page: is_search()

  • Checkingwhetherthe page is a "tag archives" page: is_tag()

  • Checkingwhetherthe post has a custom excerpt: has_excerpt()

  • Checkingwhetherit's the main query: is_main_query()

  • Checkingwhetherthe post has the given tag: has_tag()

  • Checkingwhetherthe blog is installed: is_blog_installed()

  • Checkingwhetherthe user is a "super admin": is_super_admin()

  • Checkingwhetherthe 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()

  • Checkingwhetherthe specified plugin is active: is_plugin_active()

  • Checkingwhetherthe URL is a local attachment: is_local_attachment()

  • Checkingwhetherthe page is a time-based archive page:is_time()

  • Checkingwhether the current locale is RTL: is_rtl()

  • Checkingwhetherthe page is a custom taxonomy's "archives" page: is_tax()

  • Checkingwhetherthe page is an attachment page: is_attachment()

  • Checkingwhetherthe given term exists: term_exists()

  • Checkingwhetherthe post has the given term:has_term()

  • Checkingwhetherit's a trackback: is_trackback()

  • Checkingwhether"WordPress Multisite" is in use:is_multisite()

  • Checkingwhetherthe 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()

  • Checkingwhethera menu location has an assigned menu: has_nav_menu()

  • Checkingwhetherthe specified plugin is active in multisite:is_plugin_active_for_network()

  • Checkingwhethercomments are enabled: comments_open()

  • Checkingwhethera sidebar contains any widgets: is_dynamic_sidebar()

  • Checkingwhetherthere's more than one author in the blog: is_multi_author()

  • Checkingwhetherpings are open: pings_open()

  • Checkingwhethera feed is being displayed: is_feed()

  • Checkingwhetherthe page is a "yearly archives" page: is_year()

  • Checkingwhetherthe visitor is a logged-in user: is_user_logged_in()

  • Checkingwhetherthe attachment is an image: wp_attachment_is_image()

  • Checkingwhetherthe given post type exists:post_type_exists()

  • Checkingwhetherthe 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()

  • Checkingwhetherthe specified plugin is inactive: is_plugin_inactive()

  • Checkingwhetherthe page is an "author archives" page: is_author()

  • Checkingwhetherwe're on a paged "listing" page:is_paged()

  • Checkingwhetherthe WordPress Toolbar is being displayed:is_admin_bar_showing()

  • Checkingwhetherthe page is a "daily archives" page: is_day()

  • Checkingwhetherthe given sidebar is in use:is_active_sidebar()

  • Checkingwhetherthe given username exists in the Users table: username_exists()

  • Checkingwhetherit'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()

  • Checkingwhetherthe 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!


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code