Create Another Header-Right Widget Area in Genesis Theme to Display on Certain Posts and Categories

I had to create a separate Genesis Header-Right Widget Area for a client that needed to display different elements in the widget for posts in a certain category. This can be achieved with conditional widget logic plugins but can also be done in the code.

Create the Extra Widget Area and Initialize It

//Add in new Header Right area
function cgp_extra_widgets() {
	genesis_register_sidebar( array(
	'id'          => 'HeaderRight2',
	'name'        => __( 'headerright2', 'genesischild' ),
	'description' => __( 'This is the Header Right2 Position', 'genesischild' ),
	) );
add_action( 'widgets_init', 'cgp_extra_widgets' );


 Conditionally Create the New Header for Header Right Widget 2

function cgp_remove_header() {
 if ( is_single('') && in_category( 'red' ) ) {
 remove_action( 'genesis_header', 'genesis_do_header' );
 add_action( 'genesis_header', 'cgp_special_header' );

 function cgp_special_header() {
 //my new header code
 genesis_markup( array(
 'html5' => '<div %s>',
 'xhtml' => '<div id="title-area">',
 'context' => 'title-area',
 ) );
 do_action( 'genesis_site_title' );
 do_action( 'genesis_site_description' );
 echo '</div>';

 genesis_markup( array(
 'html5' => '<aside %s>',
 'xhtml' => '<div class="widget-area header-widget-area">',
 'context' => 'header-widget-area',
 ) );

 do_action( 'genesis_header_right' );
 add_filter( 'wp_nav_menu_args', 'genesis_header_menu_args' );
 add_filter( 'wp_nav_menu', 'genesis_header_menu_wrap' );
 dynamic_sidebar( 'headerright-alt' );
 remove_filter( 'wp_nav_menu_args', 'genesis_header_menu_args' );
 remove_filter( 'wp_nav_menu', 'genesis_header_menu_wrap' );

 genesis_markup( array(
 'html5' => '</aside>',
 'xhtml' => '</div>',
 ) );
add_action('get_header', 'cgp_remove_header');

So the above is one whole function followed by an action. The first part of the function cgp_remove_header looks to conditionally remove the default Genesis header only in single posts that belong to the category red. You can string multiple conditions here or just leave it at one. So the default header is removed from those pages and a new one is created in the above example called  cgp_special_header.

The nested function cgp_special_header  then re-creates the header with genesis_markup this is found in the Genesis Framework in genesis/lib/structure/header.php lines 839-884, the new widget area is called into the code dynamic_sidebar( ‘headerright2’ );

Finally the cgp_remove_header is actioned by the get_header function.

Full Gist

References Sridhar Katakam and Ryan Meier

Leave all Comment