Customize your WordPress site with the Plugin WidgetLogic
Antonella | Sunday, August 8th, 2010 | No Comments »
One of my most used WordPress plugins is WidgetLogic. If you are looking to give your sidebars variety, you will add this plugin to your core set. As you can see from the example, WidgetLogic gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on.
In this example, the particular text widget will only appear on the home page. You can set the widgets to appear on certain pages, individual posts, individual categories and more. You can also set a widget to NOT appear in any of these particular areas, useful if you only want to omit it in a few areas.
To truly take advantage of the plugin, you don’t have to be an expert in PHP and touch any of the PHP files, but you will have to master some basic fundamentals of WordPress conditional tags. If this is new to you, review the WordPress codex for conditional tags and you will begin to understand how they work. Conditional tags are used in programming WordPress pages to show what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. That same logic is also used for WidgetLogic.
To set the post, page or category identifier, you can describe the title, slug, or post ID. You can view the post id by hovering over the item and taking note of the ID that appears in the status bar of your browser window. There is also a useful plugin Reveal IDS for WP Admin that will show the ID number on the side of each entry.
The following are some of the conditional tags I’ve used most in the WidgetLogic field.
Single request on home or front page:
is_home() is_front_page()
A single post page:
is_single()
is_single('17')
is_single(‘Summer Recipes’)
is_single(‘summer-recipes’)
(The first example applies to all posts, the second through fourth examples apply to a single post: the second example uses the page id of a particular post, the third the post title, and the last the post slug.)
A static page:
is_page('160')
is_page('About Us')
is_page('about-us')
To indicate a series of posts:
is_single(array(2,15,23))
(Note: the id number is used here, but you can also use the titles or slugs as well.)
To indicate a series of pages:
is_page(array(66,69,169,424))
To indicate a series of pages that share the same template:
is_page_template('gallery.php')
I found this useful for including a page and all of its sub-pages without having to enter exact ids every time I added a new subpage.
Customizing categories:
is_category(5) is_category(array(6,8,12))
In these two examples, the widget will ONLY appear on the category browse page, not on the single posts within the categories. To display the widget on all posts within a category or categories you will need to create more complex statements:
is_category('recipes') || (is_single() && in_category('recipes'))
To include a widget within several categories and all their single posts, concatenate the conditional statement above separated by the || function:
is_category('summer-recipes') || (is_single() && in_category('summer-recipes')) ||
is_category('cooking-tips') || (is_single() && in_category('cooking-tips')) ||
is_category('beauty-tips') || (is_single() && in_category('beauty-tips')) ||
is_category('health') || (is_single() && in_category('health')) ||
is_category('uncategorized') || (is_single() && in_category('uncategorized')) ||
is_category('news-events') || (is_single() && in_category('news-events'))
Excluding widgets:
To exclude a widget from a particular page or category, you simply place an exclamation mark (!) in front of the conditional tag:
!is_page('160')
!is_home()
!is_category(‘news-events’) || (is_single() && in_category(‘news-events’))
.
WidgetLogic is a very powerful plugin. Just one word of warning: If you change themes or de-activate your the plugin for any reason, you can lose your settings stored in the widgets. Be sure to save a copy before doing so.
Ready to customize your sidebars? Download and try out this plugin. Use any other plugins successfully to vary your widgets? Drop a note in the comments and let us know.
