I've already talked about conditional tags in other threads, but they are all in Bulgarian. You can take a peek on them though using google translate for some of the texts, but you are only interested in the examples
http://www.predpriemach.com/showthread.php?t=26088
http://www.predpriemach.com/showthread.php?t=21657
So what we need here is the usage of
conditional tags and the sidebar.php In other words - it can't be done without some coding.
First we will need to register new dynamic sidebars (
http://codex.wordpress.org/Function_Reference/dynamic_sidebar), so will put this in our function.php file of the template
PHP:
add_action( 'widgets_init', 'category_sidebars' );
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 . ' widgets',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '',
'after_title' => '',
) );
}
}
Now we have widget areas for all of our categories
Now we have to modify our sidebar.php to make some checks. I am using TwentyEleven theme. You should know that this is some kind of theme specific, so you should make some changes.
We have the default sidebar.php
PHP:
<?php
/**
* The Sidebar containing the main widget area.
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/
$options = twentyeleven_get_theme_options();
$current_layout = $options['theme_layout'];
if ( 'content' != $current_layout ) :
?>
<div id="secondary" class="widget-area" role="complementary">
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<aside id="archives" class="widget">
<h3 class="widget-title"><?php _e( 'Archives', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>
<aside id="meta" class="widget">
<h3 class="widget-title"><?php _e( 'Meta', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<?php wp_meta(); ?>
</ul>
</aside>
<?php endif; // end sidebar widget area ?>
</div><!-- #secondary .widget-area -->
<?php endif; ?>
We have to change it so it will check if we are in category, and get the appropriate sidebar for it
This will check if the user is in category (is_category can search for the title, the slug or the id of the category. I'll put the following code just before the <?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
PHP:
<?php if (is_category()) {
$sidebar_id = sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar';
dynamic_sidebar( $sidebar_id );
} else { ?>
The first line checks if we are in category and if we are, we are searching for the exact category that we need. After that we tell wordpress to show us the dynamic_sidebar( $sidebar_id ) which is dynamic_sidebar with the id of our category.
The last line says what we should do if we are not in category, so it goes for the rest of the default actions - checking if we got widgets in the default area and if we have - they are shown, if we don't it lists the default widgets.
Now we have just to ad one more } to close our check. You should put it just before </div><!-- #secondary .widget-area -->
Done.
The whole sidebar.php should look like this:
PHP:
<?php
/**
* The Sidebar containing the main widget area.
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/
$options = twentyeleven_get_theme_options();
$current_layout = $options['theme_layout'];
if ( 'content' != $current_layout ) :
?>
<div id="secondary" class="widget-area" role="complementary">
<!-- The initial edits -->
<?php if (is_category()) {
$sidebar_id = sanitize_title( get_cat_name( get_query_var( 'cat' ) ) ) . '-sidebar';
dynamic_sidebar( $sidebar_id );
} else { ?>
<!-- End of initial edits -->
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<aside id="archives" class="widget">
<h3 class="widget-title"><?php _e( 'Archives', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>
<aside id="meta" class="widget">
<h3 class="widget-title"><?php _e( 'Meta', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<?php wp_meta(); ?>
</ul>
</aside>
<?php endif; // end sidebar widget area ?>
<!-- we need to close the else -->
<?php } ?>
<!-- DONE -->
</div><!-- #secondary .widget-area -->
<?php endif; ?>
This is it, hope that this helps you.
Part of the code is taken fromm here
http://bavotasan.com/2012/create-widgetized-sidebars-for-each-category-in-wordpress/
I've just made some modifications and explanation so it will be easier for newbies to understand it
Blinky are you happy?