How to make WordPress widgets show only on selected pages and posts

By January 14, 2010 Wordpress 20 Comments

Have you ever wanted a WordPress widget to show only on one page or post? It's extremely useful to be able to limit widgets based on where a visitor is in your blog. Not only does it help you preserve and manage your valuable screen real estate, but it also helps you organize information for your website visitors.

A good example of how this might work is by limiting ads to relevant pages. Let's say you have a banner ad that you'd like to display to promote a product, but you only want that ad to show on one post. By default, WordPress widgets don't provide a way for you to limit where they are shown. After a widget is placed in the sidebar, it's just there. You can move it up or down, but it's still going to be displayed across the entire site.

Using a few simple plug-ins, you can not only control where widgets appear, but you can also rotate ads in those spots. Below is a video that walks you through the process of adding two widgets to your WordPress blog. Those two widgets are DataFeedr Random Ads and Widget Logic. Watch the video above to see how these two plug-ins help you to control where you're ads display and how much ad space you can get from a single ad spot.

UPDATED: Thanks to Jenny over at Wholistic Health Solutions for pointing out that is_page( array( 1, 2, 3 ) ) can be used to include more than one page or post in the Widget Logic conditional code. The array ( 1, 2, 3 ) portion of the WordPress conditional code is meant to act as a list of pages / posts that apply to the widget's rules.

If you know of another way to do the same thing, leave a comment and let me know.

Charles McKeever
Open Source Marketer

About Charles McKeever

Charles McKeever is the founder of Open Source Marketer, an online marketing and mobile, web development company that helps business owners design, build, and market their businesses' online. Connect with him on one of your favorite social media hangouts.

  • peter james kirkpatrick

    Excellent Post right what i was after and Explained like a pro and solved my problem Keep up your amazing work =) found you by typing into google: make word press widget appear only on home
    (just FYI) cheers

  • Thanks dude. you really help me solve my problem about widget arrangement at any pages

  • I like your article,
    I’ve recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all …

  • Hi and thankyou! Just what I needed. I was trying out flexible widgets, but it only seemed to work when the widget didn’t have any options or input to add once dragged to the widget location.
    The widgets logic plugin solved the problem.

    Note: for multiple page selection for inclusion, the following line works is_page( array( 42, 54, 6 ) )
    (not the line is_page(42,54,6) as appears in your tutorial

    • Thank you Jenny. I’m glad the Widget Logic plugin worked for you. We use it on all of our site and all of our client sites. I would imagine that this would eventually be rolled into WordPress. It just seems like a no-brainer feature.

      Yes, you are absolutely correct, is_page( array( 42, 54, 6 ) ) is the correct format. I will update the article to reflect this. For anyone who is new to WordPress or Widget Logic, Using the array allows you to include a list of pages / posts that should be covered in the plugin. It makes using WordPress conditional codes very simple.

      Leave me a link to your website where you used Widget Logic and I’ll be sure to include it in the the post as an example for everyone else.

  • Hi again,
    Yes agreed – one would think that this functionality would be a standard feature of WP.

  • Tony

    What an AWESOME post!!!

    Thank you, soooooooo much!

  • I ended up forgoing the random/rotating ads plug-in since I couldn’t find V2 and the original plug-in was corrupted. So I just went with the Widget Logic plug-in. I excluded all of my pages save for the home page. My first time altering PHP code. Either way, your post was most awesomely helpful. Thanks for sharing.

    • Glad to hear that Donald. Thank you for sharing your own experience. Widget Logic is a standard on all of our own project websites and client sites. I would love to see the features of this plugin baked right into the core WordPress install.

  • very good tutorial.
    I like the Idea and I am thinking of posting a rating for a product.
    to make it clear I make reviews for stuff and I would like to add a rating (lets say 7.5) and I want to be placed in the widget that will only show on the specific review post. is that possible?
    I thought of making a pic that has the rating but didn’t look good.
    any ideas?
    here is an example:

    the number can be highlighted.

    • Hey Bander. Yes, you could use the widget method, but if you have a lot of web pages or blog posts with reviews you might want to look into solutions that are more closely tied to the post. I’m thinking something like a custom field or even an image included in the post might work better for you. The widget solution would require you to add the page id to the widget every time you added a new review. Associating an image with the post would be easier and as long as you kept your image names consistent you could reuse and manage them long term. How much do you know about editing WordPress themes? If you don’t know how to edit themes, you might want to hire someone who can.

  • thanks for the reply.
    the whole idea is to stay away from images because during time they will add more size to my site.
    what I like to do is as you mentioned is to add a widget for each post since its once a week. No problem.
    I want it to be professional like. just like the example I pasted.
    in regards of my knowledge to editing WP themes I do need a bit of help. :)

    • Bander, I understand about wanting to keep your site streamlined. I thought you might be dealing with a fixed number of ratings images, so that’s why I suggested images. You will have to add the widget to the spot in the theme where you want it to appear. Some themes are easier to edit than others. What theme are you using?

  • a custom one.
    I think the best way is to code a plugin to do that.

  • mamta

    Thanks. you really help me solve my problem about widget.

    thank you so much
    its good tutorial

  • such a simple solution, thank you Charles :o)

  • Jeffrey

    Excellent guide! Would love to recommend for better visibility control.