Placing Widgets Where You Want Them: "Widget Logic"

Widget LogicBefore we get started, make sure that you review the introductory video on Widgets. You will find it in your site’s administration screen on the lower left: Video Tutorials > Adding Widgets.

Although the video shows how easy it is to place WordPress Widgets into sidebars, it’s not so easy to tell them that although you want them in that sidebar, you don’t want them on every page. You may want to restrict the display of widgets to particular pages and sections, or just to your home page. How to do that when there is only ONE primary and secondary sidebar for the whole site?

For instance, most commonly, you’ll encounter one of the following:

  • You want to display a widget in the primary or secondary sidebar on your home page, but not on other pages.
  • You want to display a menu of pages on the left-hand side, but only where it’s a page, and not a post.
  • You want to add buttons or other graphics on the left-hand side (primary sidebar) in some sections, but not in others.

Enter “Widget Logic.” Whenever you open up a widget panel to edit it, you’ll see a text field at the bottom labeled “Widget logic.” It looks like this:

Widget Logic

Widget logic provides a way to tell the widget to “show up here, but not here.” All it takes is knowing some simple syntax for some statements to place into this. It’s a little bit like code (well, it IS code), but it should not intimidate you. Once you get the hang of it, it will be easy to do. And you can always refer back to this tutorial for a refresher.

Widget Logic says “If the conditions below are met, display this widget.” There are many terms called “WordPress Conditionals,” and they can be combined in all kinds of ways. Let’s start with the basics you’ll need to know. Each of the following statements should read in your mind “Display this widget in this sidebar if…”:

Basic Conditionals:

  • …this is a page:  is_page()
  • …this is a single post:  is_single()
  • …this is my home page:  is_front_page()

AND and OR operators: Multiple Conditionals

You can combine statements so that multiple conditions need to be met to display the widget (AND). or so that at least one condition among a list of conditions needs to be met (OR).

  • [conditional 1] && [conditional 2]: e.g.: is_single()&&is_category(‘news’). The double ampersand “&&” operator means “meet both of these conditions to display the widget.” This statement means “Show this on individual posts that are in the news category.”
  • …[conditional 1] ||[conditional 2]: e.g.: is_front_page()||is_single(). The double pipe “||” operator means  “meet either of these conditions to display the widget.” This statement means “Show this widget on the home page and on individual posts.”

Conditionals for Particular Pages and Sections

Every page and post has an ID number associated with it. You find it by going to either Pages or Posts and looking at the list. The ID number for each page or post is listed  on the right-hand side.

List of pages and posts with ID number displayed.

That ID can then be used in the WordPress Conditional statement to declare the page(s) or section(s) where you want the widget to display in the sidebar. In the case of an entire sub-section of the site, the ID used is the “parent page” of that section– the widget would display on that parent page and all of its child pages if:

  • …this is a particular page: is_page(5)
  • …this page is any of the following I’m specifically listing: is_page(array(43,5,10,15)) (Note that when there are multiple pages specified, you need to put the comma separated list of IDs within a second set of parentheses labelled “array.”)
  • …this is a particular section of a site: uvasom_is_tree(23) (This is a custom conditional developed for the UVA School of Medicine and means “all the child pages of page [ID].”

Hiding Widgets Using the NOT Operator

Sometimes, it’s easier to state where you want the widget NOT to show rather than where it should show. Usually, this will be when you want it everywhere but the home page, but the “NOT” operator can be used with any conditional statement.

The NOT operator is an exclamation point “!”. Placed before any conditional, it makes the conditional read like its opposite. Show this widget if…

  • …it is not the home page: !is_front_page()
  • …it is not within this section of the site: !uvasom_is_tree(ID)
  • …it is not within the news category of posts: !is_category(‘news’)

Learning More About Widget Logic

You can see that this takes some practice, but when you’ve mastered Widget Logic and WordPress Conditionals, you’ll have a powerful tool for further customizing where to put your content within the given sidebars.

For more information on Widget Logic, visit:

https://wordpress.org/plugins/widget-logic/

For more information on WordPress Conditionals, visit:

https://codex.wordpress.org/Conditional_Tags

Test your Widget Logic Expertise

If you want to test your understanding of the concepts above, and get a review of this lesson, take our quiz! Your grade is not being recorded, so, what have you got to lose?

Take the Widget Logic Quiz! >>>