The widget logic plugin allows you to show different content in widget areas based on rules on your WordPress Website.
It has multiple uses for example
- showing additional navigation in a particular section of a large website.
- adding a targeted sign up forms for particular pages and posts in a category.
- showing the shopping cart only on product pages
- adding affiliate links only for posts in a particular category
Here are some examples to help you add the code to the widget to have it showing only where you want it on your website.
On a particular page or pages
Only on the front page of your website
Only on particular pages
For example, the widget will display on the page with slug sample-page and on the page with slug sample-page-2
The || means or ie is page sample page or is sample page 2
is_page('sample-page') || is_page('sample-page-2')
or you can use the following
is_page() can take id’s, page names, slugs and even arrays with multiple values – see the documentation.
On all pages except a particular page
For example, this will display on all page except the page with slug sample-page (note the “!” at the beginning)
On a page and all its descendants
To display a widget on a page and all descendant pages ie child pages and child pages of child pages.
This can be useful for large websites that have complex navigation. For example to display a custom navigation menu in one section of a website.
In the following example, the widget will display on the page with id 2953 and all pages that have the page with id 2953 as its ancestor
global $post; return (in_array(2953,get_post_ancestors($post))) || (is_page('2953'));
A particular post
All posts with a particular tag
In particular category page
Is a single post in a category
is_single() && in_category('seo') — single post that’s in the category with this slug
On a category archive page and all single posts in that category
is_category('videos') || (is_single() && in_category('videos'))
Is a single post and in category internet marking or is a single post in category portfolio
is_single() && in_category('internet-marketing')|| is_single() && in_category('portfolio')
Is not page 1 nor 2 and not a single post with id 3 nor 4
!is_page( array( 1,2) ) && !is_single( array( 3,4) )
Show a widget on child pages, not top-level pages
global $post; return ($post->post_parent != 0);
All child pages of page 77
global $post; return (in_array(77,get_post_ancestors($post)));
For Custom Post Types
is a single post in the Custom Post Type Jetpack Portfolio
Products in Woocommerce are a custom post type called product. Therefore the following would show a widget only on single product pages
All single product pages ie is a single post and the custom post type is “product”
Woocommerce has its own conditional filters
which is the equivalent of
On a Woocommerce Product Category page
is_product_category( array( 'shirts', 'games' ) )
is_product_tag( 'shirts' )
- More conditional tags in Woocommerce
- WordPress documentation on Conditional Tags
- Read here about the importance of updating WordPress, WordPress PLugins and your Theme Framework
I offer WordPress services if you need help with your WordPress site.