How to set up different sidebars for archives and Pages in Genesis

Posted on
5/5 - (456 votes)

Looking to display

  • a specific custom sidebar (widget area container) for archives (Posts page, category/tag/author/search results etc. archives) and single blog post pages
  • widgets of custom another sidebar on static Pages

in the Primary Sidebar location of your Genesis site?

Just add the following in your child theme’s functions.php

// Register Blog Sidebar widget area
genesis_register_widget_area(
	array(
		'id'          => 'blog-sidebar',
		'name'        => __( 'Blog Sidebar', 'my-theme-text-domain' ),
		'description' => __( 'Primary sidebar for blog pages', 'my-theme-text-domain' ),
	)
);

// Register Page Sidebar widget area
genesis_register_widget_area(
	array(
		'id'          => 'page-sidebar',
		'name'        => __( 'Page Sidebar', 'my-theme-text-domain' ),
		'description' => __( 'Primary sidebar for static pages', 'my-theme-text-domain' ),
	)
);

add_action( 'genesis_after_header', 'sk_change_genesis_primary_sidebar' );
/**
 * Show custom sidebars in Primary Sidebar location for blog pages and static Pages
 */
function sk_change_genesis_primary_sidebar() {

	if ( ! is_singular() || is_single() ) { // for archives and single post pages
		// remove Primary Sidebar from the Primary Sidebar area
		remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );

		// show Blog Sidebar in Primary Sidebar area
		add_action( 'genesis_sidebar', function() {
			dynamic_sidebar( 'blog-sidebar' );
		} );
	}

	if ( is_page() ) { // for static Pages
		// remove Primary Sidebar from the Primary Sidebar area
		remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );

		// show Page Sidebar in Primary Sidebar area
		add_action( 'genesis_sidebar', function() {
			dynamic_sidebar( 'page-sidebar' );
		} );
	}

}

Now you will find two additional sidebars at Appearance > Widgets, “Blog Sidebar” and “Page Sidebar”. Drag your desired widgets into these to have them appear conditionally as defined in the code above.