Widget Logic Plugin helps to Hide or Display Widgets

Update: I prepared a 39 minutes long detailed video on using the widget logic plugin with various conditonal tags.

Display Widgets on Certain Pages

Show or Display WordPress Widgets wherever you like:

I didn’t know what existed in WordPress for my needs when it comes to display or hide widgets in specific (certain) pages, posts, category pages, archive pages etc.  Accidentally, I came to know that there was a plugin named ‘widget logic’ to help me.

Installing the plugin is not hard. It installs just in few seconds as we would normally install any WP plugins. After installation, just go to the “widgets” under the “appearances” tab on your WordPress dashboard.

Look on each widget and there you can see a box named “Widget Logic”. You have to write the conditional tags in that box to hide or display the widget in specific (certain) pages.

Widget Logic Plugin Option in WordPress Dashboard

So getting most out of this plugin depends upon how much we know about conditional tags. IF you are newbie to the phrase “conditional tags“, then do not worry about that. It is very little piece of code that could do bigger things. To show how easy it is to understand the conditional tags, here is an example. is_page()

Display the Widget

This is_page() conditional tag can be used while  we want to display or hide widgets in wordpress pages.

Now I explain you how to use this conditional tag in Widget Logic plugin. If you want to display the widget in just wordpress single pages only and not in posts, home page, categories etc, then what we have to do is, just paste the code is_page() in the “Widget Logic” box under the corresponding widget.

But in another case, if you just want to display the widget only in a specific (certain) page, for example, if you want to display the widget in “Advertise with us” page alone, then the code we will have to use is_single(’5′) where ’5′ is page id of the “advertise” page. [Here is the tutorial to find Post Id and Page Id in WordPress dashboard]

Now if you want to display the widget in more than one specific (certain) page, then the code we will have to use is_page(array(5,6)). You may use the page titles instead of the page ids. The code for this is_page(‘Advertise with us’) where “Advertise with us” is the title of advertise page.

You may also use the slug of the page instead page id, title then the code is is_page(‘advertise-with-us’) where the advertise-with-us is the slug of the page.

Hide the Widget

If you want to hide the widget in all pages, the code we will have to use is !is_page(). Note the symbol ‘!’ in the code which denotes ‘not to display’ in that case.

By this way you may use these codes !is_page(’5′), !is_page(array(4,5)), !is_page(‘Advertise with us’) etc, where you want to hide the widget in page which has id 5, in pages 4 & 5 , in page titled “Advertise with us” respectively.

Conditional Tags

Below are the few of the wordpress conditional tags for your needs.

is_page() – Already explained this in above example.

is_single() – This is for WordPress Posts.

is_category() – This is for Category archive pages.

is_tag() – This is for tag archive pages.

is_date() – This is for daily, monthly, yearly archive pages

is_archive() – This is for all category, tag, day, monthly, yearly archive pages.

is_home() – This is for home page only. If you have selected a static page as the blog front page at Settings -> Reading, then the is_home() page will not work to display the widget.

In that static front page. In this case, it will work at the page selected as “posts” page at Settings -> Reading.

is_front_page() – front page of the blog

is_sticky() – if a post selected as the sticky post

comments_open() – if comments are allowed

pings_open() – if pings are allowed

is_author() – if author archive displayed

is_search() – if search results page displayed

is_404() – if 404 page shown

is_active_sidebar() – if sidebar in active,

is_plugin_active() – if a plugin is active

is_singular – At all of these cases is_single(), is_page(), is_attachment()

If are new to WordPress, here is the Ultimate Guide to create website with WordPress

{ 41 comments… add one }

Leave a Comment

  • TheShadow August 7, 2011, 6:58 PM

    Thanks for this great tutorial.what should i add to show sidebar widget in post, category and page and except homepage?

    Reply
  • FourBlogger August 7, 2011, 8:07 PM

    !is_home()

    Use that to hide widget in homepage and showing in all other pages.

    Reply
  • TheShadow August 8, 2011, 5:26 AM

    Thank you.Done.is there a way to use without widget too ie adding code manually to sidebar.php?

    Reply
  • FourBlogger August 8, 2011, 9:07 PM

    Yes, you can add to core files but when you update the theme version, you will have to be very careful whether the update preserves the codes you manually added. I would prefer to go with widgets

    Reply
  • TheShadow August 9, 2011, 5:32 AM

    Oh,I see.Then i would go with widgets itself.and this is off topic can you tell me how to change sidebar title background?

    Reply
  • FourBlogger August 11, 2011, 7:23 PM Reply
  • rick August 31, 2011, 12:45 AM

    We are redesigning our website and will use wp. I am trying to figure out a way to have a list (images) of products on a single page – when a user clicks on a product, I would like specific product details to appear in a side widget. When the user selects another product, details related to that product would replace the previous details, etc. Could you give me some feedback on how this might be done? I can do some coding but not alot. thanks

    Reply
  • Renji September 5, 2011, 1:30 AM

    Thanks for the tutorial. Was having a bit of trouble with logic, this helped. :)

    Reply
  • John September 15, 2011, 10:10 PM

    Great tutorial! We got it to work except for one page – our Blog page! Even tried the is_page() just for that page and can’t get the widget to show there. Correct page id number and all was confirmed.

    Can you think of any reason that the widget could be blocked on one page?

    Reply
  • FourBlogger September 16, 2011, 11:16 AM

    Hi John, please provide that blog page link.

    Reply
  • John September 16, 2011, 6:53 PM

    I guess I should have thought of that :)

    http://www.bodymindawakening.com/blog

    Thanks!

    Reply
  • FourBlogger September 17, 2011, 3:13 PM

    Thanks for providing the link.

    You have set it up the http://www.bodymindawakening.com/blog page as posts page in your wordpress admin reading settings. In that case, the is_home() is the correct conditional tag to be used for that blog page. I have already mentioned this in the above post for the is_home() tag. It seems you haven’t noticed it.

    However, try it now and let me know how it works.

    Thanks
    Pascal.

    Reply
  • John September 17, 2011, 5:06 PM

    First, the “is_home()” command worked, for that page.

    However, it knocked out all the others so then the widget only shows on that page and not on all the others (so we changed it back).

    That page is listed in our “all pages” of dashboard, not “all posts”. It is also not the home page, which is set to “static front page”. I suppose you are seeing it as the home page of the blog area of the site, therefore the “is_home()” worked.

    Now, how are multiple commands used?

    We need the widget in all our regular unprotected pages “is_page(array()), that Blog page “is_home()”, and some unprotected posts – where the article says to use the “is_single()” command.

    Is it possible with this widget logic approach?

    Thank you

    Reply
  • John September 21, 2011, 4:30 PM

    Is there any way to combine and use multiple conditional tags? We still haven’t found an answer.

    Thanks!

    Reply
  • FourBlogger September 21, 2011, 4:58 PM

    There is a confusion in our previous conversation.

    I am saying that you have set the “blog” page (yes, I know it is a page, not a post). But it is displaying posts. Is it right? Because you went in to here in your wp dashboard Readings -> and set it “Posts page” as “blog”. In that case, if you want to show a widget only on that page, the tag is “is_home()” and if you want to hide a widget only on that page, the tag is “!is_home()” [Note: there is a ! mark].

    Now you are asking to use multiple conditional tags. I have clearly shown in the above video about how to use multiple conditional tags. Here is an example:

    You want to show a widget only in two pages, one is home page and another one page is your “about” page. Now this is the conditional tag format is_home()&&is_page(2) [Note: 2 is the page id number of the “about” page.

    Let me know.

    Reply
  • John September 21, 2011, 5:15 PM

    Simple solution found:

    is_page() || is_single() || is_home()

    etc.

    Reply
  • FourBlogger September 21, 2011, 6:02 PM

    Fine.

    Cheers:)

    Reply
  • John September 21, 2011, 6:25 PM

    Thanks for the help! :)

    Reply
  • John September 23, 2011, 9:51 AM

    I hadn’t thought of this before, but there is no way to get the conditional tag to work across certain pages say in a specific category (so any post added to that category would automatically be included to show the widget)?

    Or is it that every time we add a new post that we want the widget to show on we also have to add the id to the code? No automation ability here?

    Thanks!

    Reply
  • Suresh September 23, 2011, 5:11 PM

    Hai John,

    Just use the category slug in conditional tag to add widget in all posts in a particular category. Example If you have category name wordpress and the slug is also wordpress then the tag will be.. in_category ( 'wordpress' ) . then widget will be displayed all the post you published in wordpress category.

    thanks.

    Reply
  • John September 23, 2011, 8:14 PM

    That didn’t work for us. It only changed it on the category archive page (the widget showed up, so it was the correct id# used for the category) but not the posts in the category (no widget there). Unless I straight out missed something additional I was supposed to do other than simply: is_category(array(3,10,100)) for example.

    We ended up going a different route, getting out of the widget logic altogether and editing the custom_functions.php file for site-wide effects.

    Reply
  • FourBlogger September 24, 2011, 9:56 AM

    can you exactly say what pages, posts, categories you want to show the widget?

    This is definitely should not be much complicated.

    Reply
  • John September 24, 2011, 4:00 PM

    This is no longer relevant for our site because we decided instead to have the widget show when user is not logged in and not show when they are logged in site wide.

    Going back, however, we used the multiple conditional tags as described above and every ID used worked fine.

    The actual categories were listed above in is_category(array(3,10,100)) example and that resulted in the widget being displayed on those category pages but not on the posts within those categories.

    The only way we found to display on any given post was to add the ID # of that post to the list within is_single(array()).

    However, going forward this wasn’t practical because every time we add a post we would have the additional step of adding the post ID to the widget logic conditional commands.

    So, if you are agreeing with Suresh in that every post within category ID 10 (yes, the actual category ID we were trying this on) should automatically show the widget, then no, that is not what we found to be true. The widget showed on that category archive page but not on the posts within that category.

    If you check our site now you will see the email optin show on every page (and if you logged in it would not show) but this is due to editing the custom_functions.php

    Thus was a very helpful article and thread and ultimately led us to where we wanted to go. Thank you!

    Reply
  • Arank October 5, 2011, 2:01 PM

    Hi!

    This tutorial was really usefull thanks. But do you know how i can show different weather-widgets ( different city’s) per page? I have locations i give some info about and i would like to show the weather as a widget in the sidebar for that particular city. how do i do that?

    Could you help me? I installed the plug in widget-logic

    Thanks!

    Reply
  • Pascal December 2, 2011, 5:39 PM

    This can’t be done simply by using Widget Logic plugin. Widget logic plugin works based on the conditional tags mentioned in the post.

    Your requirement should be done through custom coding only.

    Reply
  • Britt October 30, 2011, 7:58 PM

    Can you use this to hide a widget if Internet Exploder is being used?

    Reply
  • FourBlogger October 31, 2011, 11:30 AM

    I guess that WordPress doesn’t have such browser specific conditional tag. So I think this can’t be done using widget logic. If anyone has idea, share here!

    Britt, I think you should go for some PHP or Java based script for this.

    Reply
  • Ronan Launay November 23, 2011, 5:39 PM

    Hi,
    i used this technique in my custom widget code to display it only on homepage.
    its working great when i add my widget in the left sidebar but for an odd reason it doesnt display on the right sidebar , as if the right sidebar wasn’t part of the homepage .
    is_home() return true in the left sidebar and return false in the right sidebar on the same page.
    Any idea on why i would get this behaviour ?
    i’m using atahualpa theme btw.

    Reply
  • FourBlogger November 24, 2011, 6:23 AM

    Show me your website. Providing a link to your site can help me to understand what happens in your widget logic usage.

    Reply
  • Ronan Launay November 24, 2011, 4:23 PM

    the website isn’t published yet but here’s some code snippet
    the widget output code :
    // Output :
    <?php echo "“;
    if(is_front_page()): ?>
    ?>
    <div style="height:px;”>

    i tried with is_home() also . same behaviour.
    when get the source from the browser i got the result of both html comment but on left sidebar the “if” condition return true and in the right sidebar it returns false.

    Reply
  • Ronan Launay November 24, 2011, 4:25 PM

    edit : the echo line return some HTML comment. but it doesnt appear in the reply as its been read as comment on your blog :p

    Reply
  • Rhys December 14, 2011, 10:09 PM

    Hi Everyone

    Firstly I want to say thanks to Pascal, “thanks Pascal”.

    However I’m a little stuck and was hoping someone can help me, I’m looking to only show a tag cloud on the homepage, any idea what I need to add for that?

    Thanks

    Rhys

    Reply
  • Rhys December 14, 2011, 10:22 PM

    It’s Ok, I’v worked it out, thanks Pascal

    Reply
  • Bee February 3, 2012, 2:50 AM

    Hey Pascal

    I went through your video but I have one query hope you give an answer to it.
    I want one banner to appear in sidebar for only one category “X”.
    And that banner should appear on all the posts (In the sidebar ofc) who belong to the category “X”. Any idea how to do this ? As i can’t mention ID, Slugs etc of every post as they are well over 200+ and would be increasing as well.

    Regards

    Reply
  • FourBlogger February 5, 2012, 7:59 AM

    Put the banner in a text widget and use the

    in_category(‘fruit’)

    where fruit is the category name. Then the all posts in the category will get your sidebar banner and not in any other pages/posts of the site.

    Hope this helps.

    thanks
    Pascal.

    Reply
  • Themespotter February 13, 2012, 2:24 AM

    wow…thanks so much for the detailed video and post.

    Reply
  • Olusegun February 18, 2012, 4:40 PM

    To be sincere, this post is really helpful. Thanks for sharing

    Reply
  • Okaztle March 6, 2012, 12:50 AM

    What a nice article, have just bookmark now.

    Reply
  • muazfaris May 19, 2012, 5:57 AM

    Thank you this info.. Im googling and found ur blog to help me solve my problem.

    Reply
  • FourBlogger May 19, 2012, 8:03 AM

    Thanks Muazafris.

    Reply
  • hinterlandg June 28, 2012, 5:13 PM

    I use the News theme on Genesis platform. 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.

    Reply