Create Widgetized Sidebars for Each Category in WordPress
by c.bavota | Posted in Tutorials | 29 comments
One of the greatest features in WordPress is the ability to create multiple widgetized areas. There are tons of widgets available in the WP plugin directory so having the ability to customize different areas of your site is a must. Registering sidebars in your theme’s functions.php file is pretty straightforward, but wouldn’t it be a whole lot easier to automate your theme to create a unique widgetized sidebar for each of your category pages? It’s actually not as hard as your would think.
All you need to start things off, is to add the following to your functions.php file:
add_action( 'widgets_init', 'category_sidebars' );
/**
* Create widgetized sidebars for each category
*
* This function is attached to the 'widgets_init' action hook.
*
* @uses register_sidebar()
* @uses get_categories()
* @uses get_cat_name()
*/
function category_sidebars() {
$categories = get_categories( array( 'hide_empty'=> 0 ) );
foreach ( $categories as $category ) {
if ( 0 == $category->parent )
register_sidebar( array(
'name' => $category->cat_name,
'id' => $category->category_nicename . '-sidebar',
'description' => 'This is the ' . $category->cat_name . ' widgetized area',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
}
Now you have a function that will loop through your categories and create a new sidebar for each. With that in place, all that’s left is to add a couple of lines of code to your sidebar.php file in order to call the appropriate sidebar on your category pages:
$sidebar_id = ( is_category() ) ? sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar' : 'sidebar'; dynamic_sidebar( $sidebar_id );
The above code will default to a sidebar with the ID ‘sidebar’ unless you are on a category page, then it will find the current category and display the category’s custom sidebar. This gives you the ability to control which widgets are displayed for each category, allowing you to have even more control over your site’s layout.



Thats super handy Chris. We just launched our new website and I will have one of our developers try this out.
Cheers for all the great content
Mark
I am working with the Suffusions WP theme. My website is http://blog.ciacouponspy.com/ On the main menu the tab labeled “Find My Store” has a drop down menu. The items listed in the drop down menu are categories. As you can see I have multiple categories and I would like to be able to customize the side bar on each of these categories. Currently from what I can tell the side bar is controlled by sidebar 1. I’m not an expert with this stuff. I wanted an easier way to define which widget went to which category and so on. I used the first code you provided and I now have each category on the right side of the widgets screen, but I am unsure as to where to place the second code. I don’t want to place it in the wrong spot and ruin everything I already have. There are many many options for this theme. Could you please help guide me through where to insert the second code?
I am not sure how those themes are put together but most themes have a sidebar.php file that controls the sidebars. The second piece of code above should replace the
dynamic_sidebar()function in that sidebar.php file.Hello, i think that i saw you visited my blog thus i came to “return the favor”.
I’m attempting to find things to improve my site!I suppose its ok to use a few of your ideas!!
Hi C: You did a great job for me a couple of years ago adapting your Magazine basic to my itstheenvironmentstupid.com. I now want to do a regular blog for a novel I wrote that just got published. I’ve set up a website and hosting account with wordpress for it at visitingvati.com.
I have a rough layout of what I’d like the blog to look like. It’s pretty basic.The only image would be the book cover on the upper left hand corner. The header text and left column should all be writable. The center column would be posts and comments, the right would be widgets.
If you’re available to do this, I could send the layout it to you — and you could tell me what you’d charge to do it. Let me know, ingrid
Great job! How can I show category widget in this category posts?
Thanks!
Please Bavota! Answer the question of Dizel. I really need that.
I don’t fully understand the question. Can you elaborate a bit?
@Bavota! I mean, if I open the post of “Category A”, there should be a “Category A” Custom Menu on the “Category A” post.
Also if I open the post of child category of “Category A”, there should be a “Category A” Custom Menu on the post.
Also Child category widgets don’t show in “Appearance/Widget” to place custom menu. Only parent category show.
Removing this line:
Will allow sub-categories to appear.
If you change the sidebar call conditional from this:
To this:
You should now have those category sidebars appear on post within that category.
Worked for child/sub-categories but on post I received the following error:
Warning: Missing argument 1 for in_category(), called in example.com\wp-content\themes\custom\sidebar.php on line 3 and defined in example.com\wp-includes\category-template.php on line 233
Really wanted this feature in WordPress. You made WordPress a perfect CMS.
Is there also a way to different external CSS for each category?
Category pages do have unique classes within the body tag so you could easily specify CSS to only affect elements on those pages.
nice tutorial
Hi. I really need help. I know nothing about code and am surprised I have something online given my non-tech skills. How do I go about changing the sidebar widgets when your tutorial scares me? I am also trying to figure out how to get multiple pages under each page. Help? Thanks
Does your site have a contact page? I’m having trouble locating it but, I’d like to shoot
you an email. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.
It is in the menu in the top right.
Hi. Is it possible to use this for taxonomies as well? I have a taxonomy called “screen”. And I am looking for a way like your function to get dynamic widget sidebars automatically for each screen.
I tried to replace parts of the code with what I know, but no luck.
I’m not 100% sure if that can be done. Have you tried messing around with get_taxonomies()?
http://codex.wordpress.org/Function_Reference/get_taxonomies
Just a little bit, but with no luck. Haven’t read all of what is what there yet. I was hoping for some guidance before I tried more.
I’ll let you know if I get it right.
I have this code
register_sidebar(array (
‘name’=>’Sidebar-Wide – Top’,
‘id’=>’widget-1′,
‘before_widget’ => ‘
‘,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”,
));
now i doesn’t wants to show specific category in sidebar how can do this..
c.bavota,
This works quite well on a category page, but I added the || in_category() that you suggested earlier, and I do not get the sidebars to appear on posts within that category.
Thanks in advance.