Site icon WP Smith

Genesis 1.8 & Genesis Layouts for Specific Custom Post Type Only

So in Genesis 1.8, you will be able to designate layouts for various parts of the site, including various post types. While in Genesis 1.8, this is a bit more convoluted than necessary, it is an appropriate small step towards the right direction to expanding and improving site layouts. To do this you will need to follow these steps:

  1. Register your layouts for that post type
  2. Adjust builtin layouts for that post type
  3. Re-Create the Meta Box for the custom post type

Now, in future versions of Genesis re-creating the metabox won't be necessary (hopefully) and there will be other extensions and features as well.

Register your layouts for that post type

So we first need to register the layout. In this example, we are going to assume that our post type is registered correctly to support, genesis-layouts. If layouts do not appear, you can simply add this line to your functions.php: add_post_type_support( 'my-services', 'genesis-layouts' );.

[php]
add_action( 'init' , 'wps_create_initial_layouts' );
/**
* Registers Genesis custom post type default layouts.
*
*/
function wps_create_initial_layouts() {
genesis_register_layout( 'top-sidebar', array(
'label' => __( 'Top Sidebar', CHILD_DOMAIN ),
'img' => WPS_LAYOUTS . '/top-sidebar.PNG',
'type'    => 'wps_employee',
) );
genesis_register_layout( 'bottom-sidebar', array(
'label' => __( 'Bottom Sidebar', CHILD_DOMAIN ),
'img' => WPS_LAYOUTS . '/bottom-sidebar.PNG',
'type'    => 'wps_employee',
) );
}
[/php]

Adjust builtin layouts for that post type

If you want to include the standard types, you will need to add the following:
[php]
add_action( 'admin_init', 'genesis_fix_initial_layouts' );
/**
* Fixes registered initial layouts to be added to a specific post type.
* Genesis comes with 6 layouts registered by default. These are:
* - content-sidebar (default)
* - sidebar-content
* - content-sidebar-sidebar
* - sidebar-sidebar-content
* - sidebar-content-sidebar
* - full-width-content
*
* @uses global $_genesis_layouts
* @author Travis Smith
*/
function genesis_fix_initial_layouts () {
global $_genesis_layouts;
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
if ( $post_type == 'wps_employee' ) {
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
'content-sidebar-sidebar',
//'sidebar-sidebar-content',
'sidebar-content-sidebar',
'full-width-content'
);
foreach ( $_builtin_layouts as $layout) {
$_genesis_layouts[$layout]['type'] = 'wps_employee';
}
}

}
[/php]
The code above only changes the builtin layouts to the appropriate custom post type if on that page. If you have multiple custom post types you could do something like this:

[php]
add_action( 'admin_init', 'genesis_fix_initial_layouts' );
/**
* Fixes registered initial layouts to be added to a specific post type.
*
* @uses global $_genesis_layouts
* @author Travis Smith
*/
function genesis_fix_initial_layouts () {
global $_genesis_layouts;
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
switch ( $post_type ) {
case ( 'wps_employee' ) :
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
'content-sidebar-sidebar',
//'sidebar-sidebar-content',
'sidebar-content-sidebar',
'full-width-content'
);
break;
case ( 'my-services' ) :
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
//'content-sidebar-sidebar',
//'sidebar-sidebar-content',
//'sidebar-content-sidebar',
//'full-width-content'
);
break;
default:
$_builtin_layouts = array ();
break;
}
foreach ( $_builtin_layouts as $layout) {
$_genesis_layouts[$layout]['type'] = $post_type;
}

}[/php]

Re-Create the Meta Box for the custom post type

Since there are plans to extend the use of type in genesis_layout_selector(), adding a filter, while a great idea and dramatically reduces this code, will most likely deprecate after only one version. So, please bear with me.

[php]
add_action( 'admin_menu', 'genesis_customize_inpost_layout_box' );
/**
* Remove the builtin meta box for specific post types & Register a new meta box to the
* post / page edit screen, so that the user can
* set layout options on a per-post or per-page basis with custom genesis_inpost_layout_box().
*
* @see genesis_inpost_layout_box() Generates the content in the boxes
* @author Travis Smith
* @return null Returns null if Genesis layouts are not supported
*/
function genesis_customize_inpost_layout_box() {

if ( ! current_theme_supports( 'genesis-inpost-layouts' ) )
return;

foreach ( (array) get_post_types( array( 'public' => true ) ) as $type ) {
if ( post_type_supports( $type, 'genesis-layouts' ) ) {
if ( $type == 'wps_employee' ) {
// Remove builtin metabox
remove_meta_box( 'genesis_inpost_layout_box', $type, 'normal' );
// Add custom metabox
add_meta_box( 'genesis_inpost_layout_box', __( 'Layout Settings', 'genesis' ), 'wps_inpost_layout_box', $type, 'normal', 'high' );
}
}
}
}

/**
* Callback for custom in-post layout meta box.
*
* Echoes out HTML.
*
* @author Travis Smith
*/
function wps_inpost_layout_box() {
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
wp_nonce_field( plugin_basename( __FILE__ ), 'genesis_inpost_layout_nonce' );

$layout = genesis_get_custom_field( '_genesis_layout' );
?>
<div class="genesis-layout-selector">
<p><input type="radio" name="_genesis_layout" id="default-layout" value="" <?php checked( $layout, '' ); ?> /> <label class="default" for="default-layout"><?php printf( __( 'Default Layout set in <a href="%s">Theme Settings</a>', 'genesis' ), menu_page_url( 'genesis', 0 ) ); ?></label></p>

<p><?php genesis_layout_selector( array( 'name' => '_genesis_layout', 'selected' => $layout, 'type' => $post_type ) ); ?></p>
</div>

<br class="clear" />

<p><label for="genesis_custom_body_class"><b><?php _e( 'Custom Body Class', 'genesis' ); ?></b></label></p>
<p><input class="large-text" type="text" name="_genesis_custom_body_class" id="genesis_custom_body_class" value="<?php echo esc_attr( sanitize_html_class( genesis_get_custom_field( '_genesis_custom_body_class' ) ) ); ?>" /></p>

<p><label for="genesis_custom_post_class"><b><?php _e( 'Custom Post Class', 'genesis' ); ?></b></label></p>
<p><input class="large-text" type="text" name="_genesis_custom_post_class" id="genesis_custom_post_class" value="<?php echo esc_attr( sanitize_html_class( genesis_get_custom_field( '_genesis_custom_post_class' ) ) ); ?>" /></p>
<?php
}
[/php]

Now you have specific layouts only available for specific post types!! Enjoy!