Blog

Make Widgets Only Appear On Certain Pages In WordPress


One of the great things about WordPress is the many free plugins. Widget Logic is a WordPress plugin that I use on almost all my websites. In a nutshell, Widget Logic lets you control the pages that your widgets appear on.

For example, there are thousands of pages on one of my websites candycrush-cheats.com. These pages include step-by-step walkthroughs of every level of the original game (Candy Crush Saga), as well as all the other versions (Candy Crush Dreamworld, Candy Crush Soda, and Candy Crush Jelly). I used the Widget Logic plugin on this website so that widgets in the sidebar for the original game did not show up on posts/pages for other versions of the game, and vice versa. That is one of the best uses of the plugin to control the posts widgets appear on based on post category.

Once you install the Widget Logic plugin, you will see a new field appear at the bottom of every widget under Appearance > Widgets. The code that you add into the Widget Logic field can be a combination of PHP and WordPress conditional tags. I will include some popular code variations below, but for more examples check out the plugin page.

To make a widget appear on posts only in a particular category:

is_single() in_category(candy-crush)

In this example, the widget will only appear on single posts that have the category with the slug candy-crush.

Another good use of the Widget Logic plugin is when you want a widget to appear only on a single page, or not appear on a single page. For example, many times I will include navigational links in a sidebar widget. However, I may include these same navigational links on another page on the website or on the homepage. So I want to make sure that the navigational widget does not appear on the navigational pages in order to avoid repetition and clutter.
To make a widget appear on one single page:

is_page(levels)

Now the widget will only appear on the page with the title levels.

To make a widget not appear on a single page:

!is_page(levels)

Now the widget will NOT appear on the page with the title levels.

To make a widget appear on the homepage:

is_home()

To make a widget not appear on the homepage:

!is_home()

You will see in the above examples that adding an exclamation point (!) reverses the logic. That is making use of WordPresss conditional tags. You can vary and combine code using WP conditional tags. Some of the most useful of the WP conditional include:

  • ! (NOT)
  • || (OR)
  • (AND)

Read about all of WordPresss conditional tags here.

Widget Logic can be used for lots of things other than making widgets appear or not appear on certain posts within a category or single pages. You can use the plugin to make widgets appear only when a user is logged in, on category archives, and in a whole bunch of other scenarios. Just use the code samples and links above to get started!


Leave a comment

Your email address will not be published.