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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
}); | |
} | |
} |
Leave a Reply