If you add any widget to your site, it will be displayed in all the pages of your site by default. But there is necessity to display some widgets only on certain pages like advertisements, announcements and much more. WordPress has provided conditional tags to overcome this problem. So, if you want to show widgets only on certain pages, just follow the below steps. In the below example, I configure widget to display only on category pages and not in any other pages:
- Download Widget Logic plugin from WordPress and install it in your site and activate the plugin.
- After activating the plugin, navigate to Dashboard–>Appearance–>Widgets.
- Choose any one of the widget, which you want to show only on category page.
- Now you can find a small text box below every widgets with label “Widget Logic”.
- Enter is_category() in the text box without double quotes as shown in the below image.
From now, that widget only appears in the category page and it will not be displayed in any other pages. If you want to display the widget only on one category page, then do the following steps:
- Find that particular category id. (If you don’t know how to find, then read this)
- If the category id is 9, then enter is_category(‘9’) in the widget logic box.
From now that widget will appear only on that particular category page.
- If you want to display widget in more than one category page, then use conditional tag as “in_category( array( 1,2,3) )“.
- If you decide not to display on certain category pages then just add “!” in front the conditional tag as “! in_category( array( 1,2,3) )“.
- If you want to display widget in both category page as well as in home page then add “&&” between them like “is_category() && is_home()“.
Most Used Conditions
Is_home() – only on homepage.
Is_category() – only on category pages.
Is_single() – only on post pages.
Is_page() – only on pages.
Is_search() – only on search page To find all the conditional tags, just go to Conditional Tags Codex.