Free Tips & Tricks!

Join the others who've found my articles helpful! Get free goodness delivered to your inbox each time I share something new.

p.s. I'd gnaw my arm off before selling your email address.

Use a Custom Sidebar on a Custom Post Type

I’m working on a site where I’ve created a “Jobs” custom post type (or CPT). I wanted to display a different sidebar on my single job post pages without using a custom post template.

I found my answer after getting a push in the right direction from this post by Travis Smith and the newly created Google+ Community for Genesis WordPress. I’ll post my solution here if you’d like to use a custom sidebar for your custom post type!

1. Register Your Sidebar

Genesis-Extender-CustomFor starters, I registered a sidebar to use on my single job pages. You could put this code in functions.php (or wherever you add your custom code). I’m using the Genesis Extender Plugin (aff link), and dumped the following into my Extender Custom functions area.

Note: If you need a lot of custom sidebars for a site, you might want to use the Genesis Simple Sidebars plugin instead of individually registering each one.

// Register new sidebar
genesis_register_sidebar( array(
	'id' => 'job-single-sidebar',
	'name' => 'Single Job Sidebar',
	'description' => 'This is the sidebar for single job pages.',
) );

Sidenote for people using the Genesis Extender Plugin:

If you’re using Genesis Extender to register your sidebar, be sure you have checked the little box that says “Effect Admin.” That enables the code you enter there to literally affect the WordPress admin area. If you don’t check it, you’ll be banging your head against a wall wondering why your sidebar hasn’t shown up in the widget area. :) Genesis Extender Effect Admin

2. Call in the Sidebar Conditionally on Your CPT

Here comes the fun part. Go back to the place where you add your custom code and insert the following:

add_action('get_header','cd_change_genesis_sidebar');
function cd_change_genesis_sidebar() {
    if ( is_singular('jobs')) { // Check if we're on a single post for my CPT called "jobs"
        remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar
        add_action( 'genesis_sidebar', 'cd_do_sidebar' ); //add an action hook to call the function for my custom sidebar
    }
}

//Function to output my custom sidebar
function cd_do_sidebar() {
	dynamic_sidebar( 'job-single-sidebar' );
}

3. The End Result

Since the “jobs” site is still a work in progress, I can’t post a live link. But here are a couple of screen shots to show a regular page with the default sidebar and my single custom post type page with a custom sidebar.

Example of default sidebar

This is a regular page with the default sidebar.

custom sidebar example

CPT page for “Jobs” sporting a custom sidebar.

By the way, go ahead and get a little jealous that I’m pulling in post data on the custom sidebar. That’s an example of something easy to do with the WP Types and Views plugins (another aff link, sorry). I’ll save a write-up on using post info outside the loop for another day.

Try it Out

If you’re working with custom post types and want to take this code for a spin, please let me know how it worked for you or if you would recommend any tweaks. Have fun stormin’ the castle!

Comments

  1. Thanks for sharing Carrie! Looking forward to your post on Types and Views – I haven’t had the time to explore that plugin in detail :)

  2. thanks for the great info, Carrie!

  3. Hi Carrie,

    This was really useful, but do you know how to add a the same dynamic sidebar to the CPT Archive page?

    Thanks!

  4. Use genesis_widget_area() not dynamic_sidebar() for Genesis driven sites.

    • Carrie, Travis,

      Thank you both for helping. I know using Simple Sidebars and CPT archives added some complexity. I had tried using is_post_type_archive, which I thought was right, but I was missing the genesis_widget_area code – which solved the problem.

      Thanks a bunch!!!

      Lucas

    • Thanks, Travis! I was using dynamic_sidebar() based off some code I’d pulled off a Genesis Child Theme. When/why is it preferable to use genesis_widget_area()? Appreciate the info!

  5. What if I don’t want any sidebars to display on my custom post archive?

    • Just apply full width content layout to the archive. Add this near the top of your template page:

      /** Force full width layout */
      add_filter( ‘genesis_pre_get_option_site_layout’, ‘__genesis_return_full_width_content’ );

  6. Daniella says:

    This was great! I used the code but the new sidebar is showing below the primary sidebar. How do I omit the primary sidebar and just use the new one? Thank you!

Trackbacks

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

  2. [...] Google led me first to Carrie Dils’ easy tutorial on how to set up sidebars for a custom post type. That was my first step and probably the easiest to get sorted. I knew I wanted the custom sidebars [...]