Customizing WordPress category sidebars

Brum

New Member
Sorry for writing you once again with a problem, but I see a lot of advance people here. Me question today goes like this - Hot to customize sidebars for every single category in my WordPress blog. Or fist I should ask - Is there any chance to do it without a lot of coding? I was looking for a proper plugins, but I found nothing. Yes, there are one - Custom sidebars - but works only for pages and post, which is not what I need.

For example, I have URLs like site.com/category/name-1 and site.com/category/name-2. And both have the same sidebars. What I need site.com/category/name-1 to have sidebar-1 and site.com/category/name-2 to have sidebar-2 with different widgets within every single one. Wow, hope to understand me, because the explanation is given the way I can image in my mind. :)

I am also using Joomla CMS, and there is such options, but haven't found similar in WordPress. Does anyone has experience with this and did it before? Would love to find some solution here.
 
От: Customizing WordPress category sidebars

Sorry, dude. But as far as I know, no plugins will do the job. The only thing I have found as a solution is coding the category.php

Код:
if ( is_category('39') ) {
  get_sidebar('2');
} else {
  get_sidebar();
}
?>

or try this one:

Код:
<?php
if ( in_category('39') ) :
    get_sidebar('two'); //this will include sidebar-two.php
else :
    get_sidebar();
endif;
?>

Also there is a link explaining it best: http://codex.wordpress.org/Function_Reference/get_sidebar

Hope to see someone more familiar with this give his opinion. :)
 
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

demo1.png

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? :)
 
Последно редактирано:
Customizing WordPress category sidebars - Problem Solved

Blinky are you happy? :)
Yes, I am. I could not be more happy. :) I do you know why? Because wherever I looked, there is no such a place with so detailed information. Many thanks to @ktomov, one WP guru here. :)

You have given out too much Reputation in the last 24 hours, try again later.

@Brum, hope this will work for you. ;)
 
@ktomov, thanks a lot. That seems to be the right coding. Will follow all of the writen above and give a feedback here. :)

P.S: You have a reputation from me, once it is available. ;)
 
It's not so difficult, you only need some basic coding skills and enough knowledge of the wordpress codex. Everything you need is there - well documented and with lots of examples.

And a bit of offtopic. Blinky, can we have something like this? http://www.vbulletin.org/forum/showthread.php?t=242733 ? The purpose - it will be easier for the users to follow if someone mention them in some thread. If someone wrote @Blinky for instance, it will became a link to your profile and you'll get notification with a link for the thread.
 
It's not so difficult, you only need some basic coding skills and enough knowledge of the wordpress codex. Everything you need is there - well documented and with lots of examples.

And a bit of offtopic. Blinky, can we have something like this? http://www.vbulletin.org/forum/showthread.php?t=242733 ? The purpose - it will be easier for the users to follow if someone mention them in some thread. If someone wrote @Blinky for instance, it will became a link to your profile and you'll get notification with a link for the thread.
Once again, many thanks to @ktomov.

We will see that module and add it. Looks great. Thanks for that.
 
Once again, many thanks to @ktomov.

We will see that module and add it. Looks great. Thanks for that.

That is a great idea. Hope the plugin works fine with your version. It will show up in your notifications, once someone mention you with @ + nickname. Cool, that way people will want specific help fro someone, who they think can help and mention them in the post. Sounds great, really. :)
 
That is a great idea. Hope the plugin works fine with your version. It will show up in your notifications, once someone mention you with @ + nickname. Cool, that way people will want specific help fro someone, who they think can help and mention them in the post. Sounds great, really. :)

I've saw that in XDA dev forum and thought it will be a great addition to our forum, but I'm not completely sure if the free version is 100% usable for us. The paid offers some extras. I guess we have to wait and see what Coolice will say.
 
Последно редактирано:

Горе