• Resolved David Borrink

    (@davidborrink)


    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' );
                     }            
    }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Conditional sidebars for specific categories and their single posts’ is closed to new replies.