How To Add Hide Widgets On Certain Pages, Categories And Posts In WordPress

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.

widget logic plugin for wordpress

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_single() to display the widget only on single pages 

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.

category ID in wordpress

If you are looking for more conditional tags drop in your comments.

July 20, 2010. This entry was posted in Tutorials, WordPress and tagged . Bookmark the permalink.

We Recommend HostGator Hosting

Bloggermint strongly recommends Hostgator Hosting for all of your web hosting needs. Sign up today for WordPress Hosting at just $4.95/month.

Use coupon code "bloggermint" to get 25% discount on any hosting packages. Get an account with Hostgator now!

35 Responses so far!

  1. Wow, cool post Franklin. I did not know it. I will surely try it on my blogs. Thanks for sharing.

  2. @Nicola – Using this plugin you can make wonders with the widgets

  3. Pingback: Tweets that mention How To Add Hide Widgets On Certain Pages, Categories And Posts In Wordpress -- Topsy.com

  4. Thanks for the share Frank! i like the
    strpos($_SERVER['HTTP_REFERER'], “google.com”)!=false This is very much useful for setting up separate welcome box for users coming from different sites!!
    .-= Karthikeyan´s last blog ..How to paste text in command prompt =-.

  5. Amiable post and this enter helped me alot in my college assignement. Say thank you you on your information.

  6. Nice site. I just bookmarked you on my bloglines.

  7. Hi Franklin,

    Thanks for this info, it has finally let me organize my sidebar after months of unlucky search.

    I do have a question though. I wanted to use the “is_home()” condition but it didn’t work- I assume that the reason is that my home page (landing page) is actually unnamed in the admin panel. It is the only way I could think of of not haviing 2 home pages. Since there is no”home” page the condition can’t perform. Is there anyway around this?

    thanks,
    Federico
    maitravelsite.com

    PS I have now set it up for some things to only show in the single pages, but it would be better if they should in all but the home (landing) page

  8. Hye there, my questions is how to ignore widget in certain category i have try to put”
    !is_category(16) but did’t works..please help.

  9. Hi can I have the same widget twice in the sidebar and tell one to show up on one page and the other one on another one.

    Its a gallery widget and I want different specific thumbnails on different pages.

    I want the same widgets on all pages but with different information.

  10. One of the most valuable plugins i’ve found to date. Been researching for months to get this feature and your plugin made something that seems so hard so simple. Thanks much I will donate.

    Will recommend it.
    .-= islandyute´s last blog ..Photo- Shakira And Dizzee Rascal Burn Up New Video “Loca” =-.

  11. This is brilliant! Thanks so much. I was trying to display a login box only on the members category page. This will do the trick nicely. Cheers.
    .-= Mike Haydon´s last blog ..Use WordPress Plugins WP YouTube &amp Youtube Thumbnailer Together =-.

  12. Thanks this is exactly what I was looking for. Got it all up and working fine on one of my websites. I’m sure there could be a lot of uses for this when optimizing a blog.
    .-= Buzz´s last blog ..T-Mobile Kids Talk Free Promotion =-.

  13. I use Widget Logic and love it, but can you tell me how I would put a widget on the home page and also on one category (the blog)?

  14. Viola! cool widget, this is what i looking for. Thanks dude!

  15. I want a widget to only show on category 4 pages, so I used is_category(4)

    But I see the widget on all pages. Did I do something wrong?

  16. Pingback: How to Hide or Show Widgets on Certain Wordpress Pages, Posts and Categories - Wordpress Arena

  17. This was really great, thanks for the help Franklin!

  18. I am having the same issue as Chris. WL is working on the site, but I am trying to set on posts on a given category. Tried everything but the best I get is the widget showing up on the category main page where all the posts are with the excerpts, but not on the specifi posts on that category.

    Thanks for any help.

    Amazing work.

  19. Bro.. Is there any such tool/script for Blogger??
    Please Help..!!

  20. There are other options for this. I see many comments talking about categories. This plugin is great for categories:

    http://wordpress.org/extend/plugins/conditional-widgets/

    Full Disclosure: I wrote it :P but it’s free and actively supported. Check it out!

  21. franklin, nice article. if you could put one of these functions in context, it would be very helpful. like I have a widget that I want to show only on the front page – that’s already taken care of. but I also want to show it only when the user is not logged in. I know the programming logic but when I try to implement it, I get an error. so what I would like it a sample code that works, not just the functions. thanks.

  22. I was trying to make my main blog page look different then a category blog page. This plugin helped me get the job done. I think some of you may have been looking for a similar solution to the problem I had I would recommend three plugins that helped me to get the job done: Advanced Category Excluder, Widget Logic, and Redirection. It was a matter of being able to exclude certain posts from our main blog page (Advanced Category Excluder) then being able to exclude sidebar menu widgets from certain categories (Widget Logic). In order to get two different sidebar widgets on two pages in the blog I had to add two widgets and use in_category(x) in one and is_page(y) in the other. The Redirection plugin allowed to direct the blogs category link that is part of our theme back to the main blog page.

    This blog helped me get going in the right direction and I hope these plugins and my explanation will help someone else out. If you are curious about what it looks like then check out http://www.creeksidebiblechurch.org/sermons (this is a category page) and http://www.creeksidebiblechurch.org/blog (this is our main blog page).

  23. Thanks for this post. Any idea what tag I’d use to exclude a certain category (ie, the widget won’t be displayed in a specific category)?

    Thanks!

  24. fyi..I figured it out – you just need to add ! before the tag. So to exclude a widget on a category with the id of 30, for example, the wdiget logic tag would be: !is_category(30)

  25. I’m trying to make a conditional statement that says:

    In categories X,Y but not on single post Z

    I’ve tried a number of variations but can’t seem to figure it out. Here’s what I have now: in_category( array( 1,2 ) ) || ! is_single(11)

    I can’t seem to find an answer in the Codex, any suggestions?

  26. Quick question if I want to display a widget in all posts of subcategories of main category what would be the condition
    I have tried array with no luck, I can get it to display in post of single category with success but want it to show in multiple categories
    example
    is_category(‘forex-course’) || is_single() && in_category(forex-course’) This works for single category and posts in that category
    I have tried for multiple categories and their associated posts
    is_category(‘array(forex-course,forex-e-zine,forex-expert-advisors)’) || is_single() && in_category(‘array(forex-course,forex-e-zine,forex-expert-advisors)’)

  27. I am having some trouble getting Widget Logic to work completely on my site (http://hinterlandgazette.com). I have a customer who wants to run his ads only on the Crime and Justice section of my site. I got the code to work with in the header widget box:

    is_category(‘crime-and-justice’) || (is_single() && in_category(‘crime-and-justice’))

    I have a second widget box in the header to run all Adsense ads on other sections and I used:

    ‘is_category(‘crime-and-justice’) || (is_single() && in_category(‘crime-and-justice’))

    When I go back to the site and click on the individual posts in Crime and Justice, the Adsense ads pop up and not the customer’s ad. What did I do wrong?

    I moved my blog from Blogger to WordPress and used the customer URL permalink (/%year%/%monthnum%/%postname%.html). Not sure if that’s the problem.

    Please help, I’m about to lose a customer.

  28. Thanks for this post. I want to make a widget on post by hacking wordpress theme.. Any idea?

  29. Hi!

    I would like to know what to do if I dont want to show a widget in two pages
    I have done it for this one

    !is_page(‘teachers’)

    but I want to hide it as well in contact-us page

    Thanks for help in advance!

  30. Pingback: Widgets On Certain Pages, Categories And Posts In Wordpress | Wasamara

  31. Actually i am one of the people who don’t like to use too much plugin, but this plugin is really different.

    From the size it is really small enough, this plugin also “teach” us to understand more about wordpress

    It ask the user to write down directly the conditional tags, that’s why this plugin is not too heavy

    Thanks for sharing this information, really nice and helpful for me :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>