Conditional sidebars for specific categories and their single posts
-
My attempt at something sort of works, and could I ask for a fresh set of eyes to see what I’m missing? Thanks. I’m trying to create some conditional sidebars using Genesis Simple Sidebars, and modifying a code snippet from WPBeaches which was posted in 2014.
What I want to do is have a few custom sidebars dedicated to a few specific category archives and their single posts. I created a single test widget in each custom sidebar, wrote up the code to do two different categories with conditionals (using is_category for archive pages and in_category for single posts), and a fallback of the primary sidebar for all other categories. The result is that the sidebars work on those two categories (and one of them is a parent category), but on single posts in those two categories the result is the primary sidebar. I notice that the child category archives of the parent category do not show the custom sidebar (and I’d like them to). So it sort of works, but not as I expected. I have a conditional of is_single, is_category, and is_tag to start the process. Here is the code.
// Custom sidebars with conditionals for post categories add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); // starts the ball rolling 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', 'sb_add_sidebar' ); // adds alternative sidebar in function below } } // Alternative Sidebar function sb_add_sidebar() { if( is_category('christian-faith') || in_category('christian-faith') ) { dynamic_sidebar( 'faith-sidebar' ); } elseif( is_category('learning-and-homeschooling') || in_category('learning-and-homeschooling') ) { dynamic_sidebar( 'homeschooling-sidebar' ); } else { dynamic_sidebar( 'sidebar' ); } }
-
Additional note: the sample widgets I created in the two categories are showing up twice in each sidebar. Not sure why that’s happening. On the first I’ve got two of the sample widgets. On the second, where I have a sample I also have some additional regular widgets, but the primary widget area is Here are links to the two example category archive pages that I mention above.
This code is also duplicating my widget area on other categories.
https://sallieborrink.com/category/christian-faith
https://sallieborrink.com/category/classroom/-
This reply was modified 5 years, 3 months ago by
David Borrink.
Brain lapse. I didn’t realize (and I’m a veteran user) that categories HAVE a sidebar choice option. LOL.
I bet many of you have done this, too.
Thank you for the update I am glad you resolved the issue.
Kind regards,
-
This reply was modified 5 years, 3 months ago by
The topic ‘Conditional sidebars for specific categories and their single posts’ is closed to new replies.