How many of us want to hide or show widget on certain posts or categories or visitors coming from certain websites and its quite essential to show up your affiliate ads or a poll based upon the a particular page or a category. I am not gonna write a code for these functions rather I found a pretty cool WordPress plugin named Widget Logic that adds the ability to hide or show widgets on your WordPress blog.
So this is not actually a tutorial, but a tip to add some logic to the widgets that are used on your WordPress blog. The functions of this plugin uses the WordPress conditional tags.
Once you have installed the plugin, you can see the Widget Logic text box added to all the widgets and now using the functions you can add conditional tag to hide or show the widgets on certain pages or categories or posts. If you are not much aware of WordPress conditional tags, you can have a look at it here
To make this post pretty helpful I have added some common tags that hides or shows widgets and here we go.
is_home() to display the widget only on home page
is_single(12) to display the widget on the post ID 12. The post ID is nothing but a unique number that was allocated to a blog post. So how to find the post? Simple one of my fellow blogger have blogged about it, just head over there.
!is_home() to display the widget on all the places except the homepage.
strpos($_SERVER[‘HTTP_REFERER’], “google.com”)!=false to display the widget for visitors who comes through Google. You can try the same step to replace Google.com with Facebook.com if you want to have a greet message for Facebook users.
is_single(‘Blogger Tips’) to display the widget on the post titled Blogger Tips. This indirectly helps you in adding a suitable affiliates ads for certain pages.
is_page(‘contact’) to display the widget only on a particular page contact. You can change the value “contact” to any page name of your wish.
is_category(array(5,9,10,11)) to display the widget only for certain category classified by category numbers*. You can even use single category numbers to hide or show widgets.
is_user_logged_in() to display the widget only for logged in users.
*To know category numbers in WordPress head to the category page in your Wp admin and click on edit on any of the one category. Look into your address bar to find the category ID.
If you are looking for more conditional tags drop in your comments.