Skip to content

Instantly share code, notes, and snippets.

@nciske
Last active November 29, 2016 22:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nciske/6e2cc398f270d34207d5 to your computer and use it in GitHub Desktop.
Save nciske/6e2cc398f270d34207d5 to your computer and use it in GitHub Desktop.
Add a custom simple sidebar to a page, CPT archive, singular post, etc.
<?php
// Hooks to remove default sidebar and add our custom one
// Place me in functions.php or above genesis(); on a page template (in which case, no conditional is needed)
add_action('get_header','example_change_genesis_sidebar');
function example_change_genesis_sidebar() {
// change the conditional to determine where the swap occurs
// https://codex.wordpress.org/Conditional_Tags
// is_home() // blog (or "posts") page
// is_singular('post') // all single posts
// is_singular('page') // all single pages
// get_post_type() == 'my-cpt' // all custom post type pages (single and archive)
// is_post_type_archive('my-cpt') // Post type archive for 'my-cpt'
if ( is_singular('post') ) { // conditional to change
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar
remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // remove the 'simple sidebars' sidebar if present
add_action( 'genesis_sidebar', 'example_do_sidebar' ); //add an action hook to call the function for the custom sidebar
}
}
//Function to output the custom sidebar
function example_do_sidebar() {
dynamic_sidebar( 'my-custom-sidebar-id' ); // ID of sidebar created in Simple Sidebars or added via genesis_register_sidebar();
}
@tkanotz
Copy link

tkanotz commented Sep 22, 2015

Thank you! I will give this code a shot but it looks like it's exactly what I need :)

@Fox-Robinson
Copy link

This code adds the custom sidebar but it doesnt remove the primary sidebar, which appears below the new sidebar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment