Set a certain Sidebar to Categories and Tags or Posts with Simple Sidebars installed on Genesis

Simple Sidebars is a great plugin to use different sidebars on posts, pages, categories and tags in a Genesis theme.

Easy to set up and set for individual pieces of content. However their is no option to bulk set a specific sidebar to a certain category or tag or bunch of posts or any other set of conditionals.

If you don’t fancy doing it manually by selecting each piece of content and selecting the menu from the dropdown,  it can be applied automatically to all desired content with a couple of functions in your theme functions.php file. The example below targets all posts, categories and tags to have specific sidebar whilst leaving any page to have its sidebar as set in Simple Sidebars manually.

function themeprefix_remove_sidebar() {
	if ( is_single() || is_category() || is_tag() ) {
		remove_action( 'genesis_sidebar', 'ss_do_sidebar' );
		remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
		add_action( 'genesis_sidebar', 'themeprefix_add_sidebar' );
	}
}

//Alternative Sidebar
function themeprefix_add_sidebar() {
	dynamic_sidebar( 'category-sidebar' );
}

add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); //sets the ball rolling

Ok so we have 2 functions and an action, the bottom action triggers it all.

The first function themeprefix_remove_sidebar() selects what we are targetting, in this example it is all posts, categories and tag archive pages are selected. The 2 pipe symbols mean ‘or’ and you can change the conditional syntax to suit. (PHP and WordPress refs).

Then the Simple Sidebar  and regular Genesis sidebar are removed in the next 2 actions, the third action adds in the sidebar we want by calling the function below it.

In that function below – themeprefix_add_sidebar() the name/ID of the required sidebar is passed into the dynamic sidebar which will replace the sidebar for the desired content, category-sidebar in this example.

How you find the name/ID of the Simple Sidebar….

genesis-simple-sidebars

Full Gist with Comments

<?php
//do not copy the above opening php tag
add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); // starts the ball rolling
/**
* Conditionally Change Sidebar in Genesis whilst using Simple Sidebars
*
* @package Genesis Sidebar with Simple Sidebar Switcheroo
* @author Neil Gee
* @link https://wpbeaches.com/bulk-set-sidebar-categories-tags-simple-sidebars-installed-genesis/
* @copyright (c) 2014, Neil Gee
*/
function themeprefix_remove_sidebar() {
if ( is_single() || is_category() || is_tag() ) { // set your connditionals here
remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // removes Simple Sidebar
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); // removes Genesis Default sidebar
add_action( 'genesis_sidebar', 'themeprefix_add_sidebar' ); // adds alternative sidebar in function below
}
}
// Alternative Sidebar
function themeprefix_add_sidebar() {
dynamic_sidebar( 'category-sidebar' ); // add in the ID of the sidebar you want to replace it with
}

1 comment

  • Hi,

    Great tutorial! I was struggling with this sidebar-categories issue.

    I have a question.

    What if I would like to have two separate sidebars for two separate categories?

    I replicated this code, and here is what I have https://gist.github.com/db7d43ec1c62179a6937.git

    However this causes my homepage to have the categories “394” sidebar. Any idea why?

    I would really be thankful for a reply. great site, great tutorials!

Leave your comment