Common use snippets for Widget Logic
Widget Logic is a WordPress plugin that that gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.
I’ve been using Widget Logic in my WordPress projects for a while now – it’s clean, versatile and I highly recommend it.
Below are some snippets that you will likely find useful when you are building a new site.
Show widget only on specific pages or posts
This example displays the widget only on the page that has the slug example-page
is_page('example-slug')
This is my favourite way of specifying widgets for certain pages, as it works in a similar fashion to how you configure blocks for specific paths in Drupal.
This example displays the widget on pages with ID 1 and 5.
is_page(array(1,5))
is_page() is very flexible and can take id’s, page names, slugs and even arrays with multiple values – see the documentation.
But what about blog posts and attachments?
Use is_single() instead – see the documentation.
Here is an example that display the widget when the user is visiting a post with the title “About this blog”
is_single('About this blog')
Show widget everywhere except a specific page or post.
This is the inverse of the previous examples, so the first example would be:
!is_page('example-slug')
Note the exclamation point, which inverts the logic.
The second example would be:
!is_page(array(1,5))
Show widget only on front page
Handy for front page-specific summary widgets, calendars, introductions etc.
is_front_page()
Show widget only for a specific post type
An easy way to display widgets for certain post types.
get_post_type()=='post'
Some common post types
- page
- post
- attachment
Show widget based on a WPML language
If you are running a site using the WordPress Multilingual Plugin (WPML) you can use this snippet to only show a widget when the user is viewing the page in a certain language (English in this case)
(ICL_LANGUAGE_CODE == 'en')
There are more useful constants and functions available in the WPML manual.
Combining multiple rules
It’s easy to combine multiple rules with regular PHP logic. This example will display the widget only when the user is viewing a site in Swedish (WPML) and is on the pages with ID 1 or 5:
(ICL_LANGUAGE_CODE == 'sv') && is_page(array(1,5))
Don’t forget you can also use the || (or) operator!
More useful tags
I recommend reading the WordPress documentation on Conditional Tags to customize Widget Logic for your exact needs.
Feedback!
Do you have a useful snippets for Widget Logic? Feel free to post it in the comments!
Related
This entry was posted on 17/07/2012 at 23:01 and is filed under Computers, Development, Programming, Technical solutions. You can subscribe via RSS 2.0 feed to this post's comments.
Tags: php, widget logic, wordpress
You can comment below, or link to this permanent URL from your own site.
08/10/2013 at 18:51
Nice post – very usefull. However I need your further explanation, I mean you describe how to display exactly the same widget on certain pages. My question is how to display various widgets on various pages. Pls kindly explain.
09/10/2013 at 00:10
Hi Kris,
To display a widget on multiple pages, you can specify multiple IDs to is_page, like this:
is_page(array(1,5,18,32,47,22))
When you have multiple widgets, I usually go through them one by one, decide which posts/pages they should go on, and then use the appropriate is_page or is_post functions to place the widget on all applicable pages.
Hope that helped.
27/11/2013 at 17:12
[…] Some common uses for Widget Logic […]
05/12/2014 at 10:45
Ok—but how would I place the same widget in a page AND in a post?
05/12/2014 at 12:32
You use the OR operator, like this:
is_page() || is_single()
22/10/2015 at 20:03
Great post — thank you.
Can I display a widget on multiple blog posts AND certain categories??
Thanks,
22/10/2015 at 23:17
Sure thing, this should work:
is_single(1, 2, 3) && in_category( ‘category-1’, ‘category-2’)
15/12/2015 at 19:10
How do I make a widget show up on the main category page plus any post associated with that category?
16/12/2015 at 10:43
Something like:
is_category(1) || (is_single() && in_category(1))
(Replace 1 with your category id.)
Hope that helps!
31/03/2019 at 11:51
Thank you for putting this out. I knew there had to be a method to my widget madness.
27/09/2019 at 08:18
Hi all,
does anybody know why this snippet doesn’t work?
get_post_type() == ‘portfolio’
&& is_single()
&& has_term( array(‘my-cat-1’, ‘my-cat-2’, ‘my-cat-3’), get_the_ID() )
Thank you in advance 🙂
04/04/2020 at 22:39
The product details of the plugin were not as helpful as your well-articulated examples of Widget Logic. Thank you for your assistance in making clear the plugin’s vocabulary. Without it, I might have given up on using it and opted for something which actually would be less suited