Conditionally Swapping Genesis Sidebars

Example code showing how to remove the default Genesis primary sidebar and load a different sidebar when viewing an archive or single post from a specific custom post type.

<?php
add_action( 'genesis_before_sidebar_widget_area', __NAMESPACE__ . '\\sidebar_switcher' );
/**
* Conditionally replace the sidebar.
*
* @since 1.0.0
*/
function sidebar_switcher() {
// Switch to a different sidebar for 'project_post_type' CPT.
if ( 'project_post_type' === get_post_type() && is_active_sidebar( 'project-post-type-sidebar' ) ) {
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
add_action( 'genesis_sidebar', function(){
dynamic_sidebar( 'project-post-type-sidebar' );
});
}
}
view raw functions.php hosted with ❤ by GitHub

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *