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.