How to use Widget Logic for WordPress

Widget Logic is a powerful plugin that controls where a widget should appear and conversely where it should not. It is included on my list of first plugins to install on a site running WordPress. After you install the plugin a new input area will be added below each of your widgets. That’s where we’ll put our WordPress Conditional tags and other statements.

widget-logic-use
In this sample, the Text Widget will only appear if the blog page or posts page is being displayed.

How to Use WordPress Conditional Tags

Here are more Conditional Tags that are useful for determining widget placement

is_front_page()
Show widget on the front page.

is_home()
Show widget on the “posts page” aka “blog page”.

is_page()
Show widget on pages. You can use the Page’s ID, Title or Slug.
Example:
    is_page('About')
    is_page(50)
    is_page( array('About', 'Contact', 50, 75) )

is_single()
Show widget on posts.
Example:
    is_single('Hello World')
    is_single(45)
    is_single( array('Hello World', 'Happy Birthday Post', 45) )

in_category()
Show widget if post is in a category.
Example:
    in_category('Anime')
    in_category( array('Anime', 'Manga', 'Cat1', 'Cat2') )

has_tag()
Show widget if post has a tag.
Example:
    has_tag('Tutorials')
    has_tag( array('Tutorials', 'WordPress', 'Tag1', 'Tag2') )

get_post_type() == 'topic'
Show widget if a bbPress Topic is being displayed. You can also use is_bbpress() but this will produce an error if the bbPress plugin is disabled. I was originally finding a way to use function_exists to wrap on is_bbpress but I haven’t had any success.

You can also use get_post_type() when specific post types are in use.

Other Tips on Using Widget Logic

  • Combining statements allows you to have more control.
  • Example:
    Show widget on Home page and the About page.
    is_front_page() || is_page('About')

    Show widget on pages with a Tutorial Tag and on bbPress Topics.
    has_tag('Tutorial') || ( get_post_type() == 'topic' )

  • Negation (!) adds some flexibility and is sometimes more efficient.
  • Example:
    Show the widget everywhere but not on the Home page.
    !is_front_page()

    Show the widget on all posts but not on Pages.
    !is_page()

    Show the widget everywhere but not on the About page.
    !is_page('About')

  • If you have AdSense ads on your widgets you might want to add this condition.
  • !is_preview()
    This prevents the ads from showing whenever we click Preview on the Post Editor. This is done so that Google Bots won’t crawl the preview page and consequently report the page as missing or status 404.
Share Post :

27 Comments

Add a Comment
  1. You can use multiple Conditional Tags in it with logic operators.

    1. I may have clicked “publish” prematurely. I’ve added your suggestion 🙂

  2. Great post. I’m using the tag wp_is_mobile along with Widget Logic to attempt to customize widgets solely on mobile (ipad, phone, etc.). However, because I’m always calling wp_is_mobile() it’s hard to combine with other logic.

    For example, I’d like to show on all mobile pages except for posts. I’ve tried everything but nothing works. It would seem to me wp_is_mobile(‘page’) || !wp_is_mobile(‘post’) should work – but it doesn’t (perhaps because I need to specify the post names and cannot just block all?

    Any ideas?

    1. According to the codex, wp_is_mobile() does not accept any parameters.

      For example, I’d like to show on all mobile pages except for posts.

      Mobile pages? There is no mobile type of posts/pages. Widget Logic is for Widgets. The conditional wp_is_mobile() will return true if your post/page is being viewed on a mobile device.

      For example, let’s say you have a Text Widget.

      Example 1: wp_is_mobile()
      The conditional above means that your particular Text Widget will only appear to a user who is using a mobile device to browse your posts/pages.

      Example 2: wp_is_mobile() && is_page()
      The conditional above means that your Text Widget will only show if the user is browsing with a mobile device and is viewing a page (not a post).

  3. Hello Ron,

    Firstly thank you for this helpful tutorial. I had absolutely no clue about this conditional stuff…

    I wonder if you could help me. I am building a client website and have setup the sililoqy image slider in the header right widget area. Then my client requested static images in the header area for certain pages. I couldn’t find a solution until someone mentioned widget logic and then I found your page.

    So I used this !is_page ('Wines') to prevent the sililoqy slider from showing up on the Wines page and it worked just fine…

    As soon as I put in two conditional elements !is_page ('Wines') || !is_page ('Cigars') it no longer removes the slider on both pages. I need to actually prevent the slider from showing up on 5 or 6 pages…

    What am I doing wrong?
    Appreciate your help..
    Regards
    Greg
    South Africa

    1. Have you tried combining the pages in one conditional tag?
      Ex. !is_page( array('Wines', 'Cigars') )

      1. Hey Ron,
        Yes just tried that and it worked…you are a star!!!

        I will load all the other pages and report back.
        Regards
        Greg

  4. Hello, can you tell me right code for this:

    I want widget show All Single blog post except sticky post.

    1. You can use is_single() && !is_sticky()

  5. I have about 30 pages where I need one widget holding an image to change based on the page. There is a different image for each page (e.g. Aerospace, Appliances, Automotive, etc.)

    Is there a way to write an if clause with the page name and the image? Otherwise I foresee having to put 30 text widgets in with one page per widget and the corresponding image.

    1. Widget Logic can’t affect the content of a widget, it only dictates where or when the widget will appear. However, it is possible to achieve what you want using both Widget Logic and PHP Code Widget.

  6. Hi,

    I want that a widget should show across all pages except on two pages. These two pages should have two separate things. Is there a way to restrict things with an exception.

    1. The negation sign (!) mentioned on the post does exactly that.

  7. In my website home page i want to show only widget and no home page/or post will be in home page.i.e. I would like to disable home post from home page, how its possible.

    Thanking in advance

    1. That’s not possible with Widget Logic as it only affects widgets and not the content of a post or page. You’ll have to create your own page template that doesn’t have content.

  8. Thanks for quick reply.

  9. I’m trying to set up widget logic on a woocommerce site. I need specific filters to only show on certain product category pages.

    Do you know how I could do that?

    in_category(‘Anime’)

    Does not seem to work for woocommerce product categories.

    Thanks!

    1. Woocommerce uses a different category separate from the default WordPress category.
      Try this woocommerce conditional:
      Ex. is_product_category( 'Anime' )

  10. Hi,

    Great post! I broke my head already trying to show a widget on front page, on all categories except one category. I am doing like this:

    is_front_page() || is_category( ) || in_category(‘Category’ )

    PLEASE PLEASE HELP

    what is the difference between iS_category( ) and iN_category( )

    thanks

    1. is_category() checks if the Category archive page is being displayed.
      in_category checks if the current post belongs to a certain category.

  11. I am using the in_category but it’s making the ad show up on the homepage when the post is the first post on the page. However, I only want my front page ad to show up. Not the in_category ad too. What do I need to do to make the in_category ad not show up on the homepage?

    in_category(“mugshots”) ad is showing up on homepage when a post from the mugshots category is on the homepage (I don’t want this to ad to be on the homepage).

    1. in_category(“mugshots”) && !is_front_page()

  12. Hi Ron,

    Many thanks for a very helpful post.
    I would be grateful if you could help me with the following:
    I wanted to show adsense on all my posts except the pages so I used your tips above and put the following code.

    !is_page( array( 'about-us' , 'privacy-policy' ,  'terms-of-use' ,  'contact' , 'disclaimer') )

    It works fine except for the home page, when I add ‘home’ OR ‘front-page’ it doesn´t work.

    I would be grateful for your response.

  13. Hi Ron,

    Please help me out here.

    How can I put adsense only on the posts and exclude all the pages. I tried a code which you gave above and it works except on the home page. I tried ( home ) ( front-page )

    Thanks

    1. You don’t want to show ads on Pages and the Homepage?
      Try this:

      !is_page() && !is_front_page()

Leave a Reply

Please write your comments in english. I delete anything that even remotely resembles spam.

Note: When posting code, enclose it in pre and code tags.
e.g. <pre><code> Add code here </code></pre>