Display a custom sidebar on a custom post type

Here’s how to replace the primary sidebar on a custom post type. First, register your sidebar. Next, use this code to swap the primary sidebar with the new one you just registered:

// Swap sidebars on CPT pages
add_action('get_header','child_change_genesis_sidebar');
function child_change_genesis_sidebar() {
	if ( is_singular('post_type')) { // change 'post_type' to your CPT slug
		remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar
		add_action( 'genesis_sidebar', 'child_do_cpt_sidebar' ); //add an action hook to call the function for my custom sidebar
	}
}
 
// Output sidebar with the id 'sidebar-custom'
function child_do_cpt_sidebar() {
	genesis_widget_area( 'sidebar-custom' );
}

If you want to display the sidebar on both the single post page and archive page for your custom post type, modify the if statement in child_change_genesis_sidebar() function to include

if ( is_singular('post_type') || is_post_type_archive('post_type') ) {

Credit: Carrie Dils & Travis Smith from How to Use a Custom Sidebar on a Custom Post Type

Replace default loop with custom loop

Genesis comes with a handy function called genesis_custom_loop(). It’s an easy way for you to create a new loop without having to write all the code to create a new loop with WP_Query manually. Here’s how you can use it in your theme.

/** Replace the standard loop with our custom loop */
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_do_custom_loop' );

function child_do_custom_loop() {

	global $paged; // current paginated page
	global $query_args; // grab the current wp_query() args
	$args = array(
		'category__not_in' => 42, // exclude posts from this category
		'paged'            => $paged, // respect pagination
	);

	genesis_custom_loop( wp_parse_args($query_args, $args) );

}

The above will spit out a loop that excludes posts from category ID 42 and displays posts from the current paginated page (i.e. /page/2 and so on). The wp_parse_args() function combines the $args array with the current $query_args for that page. The $args array should accept all WP_Query parameters.

Replace with a non-Genesis loop

If you need to completely customize the loop, if you are displaying events or a ecommerce shop page for example, you don’t need to use the genesis_custom_loop() function at all. Just write your own to replace it.

/** Code for custom loop */
function my_custom_loop() {
	// code for a completely custom loop
}

/** Replace the standard loop with our custom loop */
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'my_custom_loop' );

You can stick this code in functions.php to affect all the loops in your site, or put it in a template file like date.php or author.php to limit it with the template hierarchy. If you do stick it in a template file, remember to end it with genesis().

Conditional CSS classes for IE in Genesis

Here’s how to implement Paul Irish’s solution for conditional IE CSS classes for the html tag in Genesis.

/** Conditional html element classes */
remove_action( 'genesis_doctype', 'genesis_do_doctype' );
add_action( 'genesis_doctype', 'child_do_doctype' );
function child_do_doctype() {
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--[if lt IE 7 ]> <html class="ie6" xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7" xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8" xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9" xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="" xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>> <!--<![endif]-->
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php bloginfo( 'charset' ); ?>" />
	<?php
}

Now you can easily target IE in your CSS like so:

div.foo { color: inherit;}
.ie6 div.foo { color: #ff8000; }

Simple huh?

Remove Genesis layout settings for a Custom Post Type

If you’ve forced a layout for a custom post type, there’s no need to display the Layout Settings metabox in the edit screen. Here’s a code snippet that removes the layout options for a product custom post type.

/** Remove Genesis inpost layouts from 'product' post_type */
add_action( 'init', 'child_remove_post_type_support' );
function child_remove_post_type_support() {
	remove_post_type_support( 'product', 'genesis-layouts' );
}

Replace product with the custom post type you want this to work for.

Credit: Ade Walker, gleaned from his article How to use Genesis Connect for WooCommerce.

Want to remove the default layout metabox in the Genesis Theme Settings page instead? Use this snippet: Remove unused theme settings metaboxes

Force a layout in a template

Here’s a code snippet you can use when you want to lock down a layout for a specific template. Just drop the following into a single-posttype.php, template-something.php, etc before the genesis() function:

/** Force full width layout */
add_filter( 'genesis_pre_get_option_site_layout', 'child_do_layout' );
function child_do_layout( $opt ) {
    $opt = 'full-width-content'; // You can change this to any Genesis layout below
    return $opt;
}

As a reminder, Genesis comes with 6 default layouts:

content-sidebar
sidebar-content
content-sidebar-sidebar
sidebar-sidebar-content
sidebar-content-sidebar
full-width-content

See Brian Gardner’s post on creating a landing page in Genesis for a practical example of how you would use this.

Follow

Get every new post delivered to your Inbox

Join other followers: