How to selectively display widgets on specific posts or pages

WordPress by default display the widget every where. So if you want to display or hide the  widgets based on rules then you will need to make use of plugins.

In this tutorial I will cover 4 plugins which can be used to hide or display widgets in posts or pages control the widget visibility of specific post and pages. At the end o this post I will also share a code snippet for controlling the widget visibility based on conditional logic.

1. Using Widget Logic Plugin

This is a very powerful plugin. It works by adding an extra logic field to every widget.  It lets you control the pages that the widget will appear on . Refer to this screenshot.

So for example if you want the search widget to only appear on home page , then you will simply use the is_home() tag in the logic field. Similarly if you DONT want to display the search widget on home page, you will use !is_home() tag in the logic field.

This can be configured in a lot of ways.  For example, you can display / hide widgets on certain categories , you can display widget on a certain posts etc. It is a very powerful plugin and since it uses conditional tags,  it can be a bit difficult for newbies and non technical users.

2. Using Widget Context Plugin

Widget context is very similar to Widget logic. The main difference is the user interface.  The widget Context gives you a friendly user interface to control the widget visibility. Refer to this screenshot

It is possible to display / hide widgets on specific posts, pages, categories , url etc.  It is a beginner friendly plugin as users dont require knowledge of conditional logic to operate the plugin.  So, if you are an absolute beginner , I recommend you use the Widget context plugin

3. Using Dynamic Widgets Plugin

This is another popular plugin which allows you to define visibility through a user interface.  The plugins opens a separate page to configure the visibility rule. This can be a minor inconvenience. Refer to this screenshot

Apart from that it is a good plugin and supports rules like specific categories, display on a url, specific posts, archive pages etc.

4. Using Jetpack Plugin

If you are using Jetpack plugin, then you can utilize the inbuilt widget visibility feature to control the widget display. It gives you a user friendly interface to configure the widget visibility. Visibility can be controlled based on category, post , pages, users role, date etc. Refer to this screenshot

There are other plugin like Restrict widgets and Widget Manager Light. But I am not covering them in this tutorial as they are not as popular as the above four plugins.

Code Snippet

In case you dont want to use plugin then you may use the following snippet to control the widget display.

add_filter( 'widget_display_callback', 'spice_display_widget_logic',10,2);

function spice_display_widget_logic( $instance, $widget) {  
  
  if ( $widget->id == 'calendar-4') { // we are controlling the visibility of calendar-4 widget.    
	
	if ( is_page('24')) {    // calendar-4 widget will NOT display on page with id 24          
	  return false;     }  
  }
}

This snippet ensures that the calendar-4 does not appear on a page having id 24.  Refer to this tutorial to understand how to get widget id.

You can modify the snippet to control widget visibility based on post types,  user role, etc.

Even though the above snippet works , I would not recommend using the snippet unless you are a developer. If you are non-technical user, simply use any of the above mentioned plugins.

If you have any questions,ask in the comments.

 

1 thought on “How to selectively display widgets on specific posts or pages”

Leave a Comment