There seem to be alot of blogs around the net that answer half this question, so while I remember how I did it, I thought I would put pen to paper (so to speak).
The Problem
I’m building a wordpress website of a classic “brochure” style. That is to say pages for “Home”, “About Us”, “Our Products”, “Another Page” etc…
On each of the sub-pages (not the home page), the customer would like to display a column of images but different images for each page. The column of images, as you might expect, will be implemented using individual image widgets in the sidebar.
The Solution
I quickly determined that it would be better for me to be able to turn on and off widget areas rather than individual widgets. This is because I had settled on using an image display widget that displays one image and therefore would need many occurences of this widget for each page.
So, I decided to define 6 widget areas. Widget-Area-1 would appear on every page and then widget-area-2 to widget-area-6 could be turned on and off for each individual page.
So first define the 6 widget areas in your themes function.php
| PHP | | copy code | | ? |
| 01 | function SmartWidgets_widgets_init() { |
| 02 | // Area 1, located at the top of the sidebar. |
| 03 | register_sidebar( array( |
| 04 | 'name' => __( 'Widget Area 1', 'SmartWidgets' ), |
| 05 | 'id' => 'widget-area-1', |
| 06 | 'description' => __( 'Widget Area 1', 'SmartWidgets' ), |
| 07 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 08 | 'after_widget' => '</li>', |
| 09 | 'before_title' => '<h3 class="widget-title">', |
| 10 | 'after_title' => '</h3>', |
| 11 | ) ); |
| 12 | |
| 13 | // Area 2, located below the Primary Widget Area in the sidebar. Empty by default. |
| 14 | register_sidebar( array( |
| 15 | 'name' => __( 'Widget Area 2', 'SmartWidgets' ), |
| 16 | 'id' => 'widget-area-2', |
| 17 | 'description' => __( 'Widget Area 2', 'SmartWidgets' ), |
| 18 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 19 | 'after_widget' => '</li>', |
| 20 | 'before_title' => '<h3 class="widget-title">', |
| 21 | 'after_title' => '</h3>', |
| 22 | ) ); |
| 23 | |
| 24 | // Area 3, located in the footer. Empty by default. |
| 25 | register_sidebar( array( |
| 26 | 'name' => __( 'Widget Area 3', 'SmartWidgets' ), |
| 27 | 'id' => 'widget-area-3', |
| 28 | 'description' => __( 'Widget Area 3', 'SmartWidgets' ), |
| 29 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 30 | 'after_widget' => '</li>', |
| 31 | 'before_title' => '<h3 class="widget-title">', |
| 32 | 'after_title' => '</h3>', |
| 33 | ) ); |
| 34 | |
| 35 | // Area 4, located in the footer. Empty by default. |
| 36 | register_sidebar( array( |
| 37 | 'name' => __( 'Widget Area 4', 'SmartWidgets' ), |
| 38 | 'id' => 'widget-area-4', |
| 39 | 'description' => __( 'Widget Area 4', 'SmartWidgets' ), |
| 40 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 41 | 'after_widget' => '</li>', |
| 42 | 'before_title' => '<h3 class="widget-title">', |
| 43 | 'after_title' => '</h3>', |
| 44 | ) ); |
| 45 | |
| 46 | // Area 5, located in the footer. Empty by default. |
| 47 | register_sidebar( array( |
| 48 | 'name' => __( 'Widget Area 5', 'SmartWidgets' ), |
| 49 | 'id' => 'widget-area-5', |
| 50 | 'description' => __( 'Widget Area 5', 'SmartWidgets' ), |
| 51 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 52 | 'after_widget' => '</li>', |
| 53 | 'before_title' => '<h3 class="widget-title">', |
| 54 | 'after_title' => '</h3>', |
| 55 | ) ); |
| 56 | |
| 57 | // Area 6, located in the footer. Empty by default. |
| 58 | register_sidebar( array( |
| 59 | 'name' => __( 'Widget Area 6', 'SmartWidgets' ), |
| 60 | 'id' => 'widget-area-6', |
| 61 | 'description' => __( 'Widget Area 6', 'SmartWidgets' ), |
| 62 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 63 | 'after_widget' => '</li>', |
| 64 | 'before_title' => '<h3 class="widget-title">', |
| 65 | 'after_title' => '</h3>', |
| 66 | ) ); |
| 67 | } |
| 68 | |
| 69 | /** Register sidebars by running SmartWidgets_widgets_init() on the widgets_init hook. */ |
| 70 | add_action( 'widgets_init', 'SmartWidgets_widgets_init' ); |
Next we need a funtion that will turn off a given widget area. Add this to your theme’s function.php
| PHP | | copy code | | ? |
| 1 | //register the Shortcode handler |
| 2 | function xtreme_unregister_sidebar($sidebar) { |
| 3 | unregister_sidebar($sidebar); |
| 4 | } |
Now is the clever bit, in each page we are going to define a custom variable “sidebar” that will define which sidebars to turn off. This will be a comma delimited list of sidebar IDs (see the code above where each widget are is assigned an ID ‘id’ => ‘widget-area-6′,
So in each page define your custom variable
We then need to tell the theme to turn off these widget areas. In your theme’s sidebar.php you should find a line like this
| PHP | | copy code | | ? |
| 1 | <?php get_sidebar(); ?> |
| PHP | | copy code | | ? |
| 1 | Replace it with the following code; |
| PHP | | copy code | | ? |
| 01 | <?php |
| 02 | $sidebarList = get_post_meta($post->ID, "sidebar", true); |
| 03 | |
| 04 | if ($sidebarList!="") { |
| 05 | $sidebars=explode(",",$sidebarList); |
| 06 | foreach ($sidebars as $sidebar) { |
| 07 | add_action( 'theme_sidebars', 'xtreme_unregister_sidebar' ); |
| 08 | do_action('theme_sidebars',$sidebar); |
| 09 | } |
| 10 | } |
| 11 | get_sidebar(); |
| 12 | |
| 13 | ?> |
| 14 |
And that should do it!
Please note: I’m not a wordpress guru. I extracted this howto from a number of different sources and would like to thank all those concerned for their help;
- Display different sidebar for each post and page for wordpress
- Remove widget areas without editing wordpress template
- WordPress Child Themes – Remove Widget Areas
- How to: Remove widget areas on your blog homepage without editing template files
Please let me know if you found this useful or of any improvements by adding a comment below. Thanks!

Blog
Photography
Yii Framework
My Portfolio