Sidebars-and-CPT

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. 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-Custom

For 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.',
) );

Side note for people using the Genesis Extender Plugin:

Genesis Extender Effect Admin

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. 🙂

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
Custom post type page for CPT “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!

62 thoughts on “Use a Custom Sidebar on a Custom Post Type”

  1. I am registering multiple sidebars and got errors because the name spaces were already taken. I changed these to something different and the code was accepted and the error report went away.

    Is there anything else I need to know if registering multiple sidebars as code in functions.php?

    Also, I might create the code in a new custom plugin rather than dropping it in functions.php. It is getting bloated.

    1. Hey Darren,

      Yeah, when creating custom function, ID’s, etc, it’s good to preface them with your theme or plugin text domain to avoid conflict with anything else that might be using a similar name.

      For example, if my theme text domain is “utility-pro” then I could register a sidebar id as “utility-pro-cpt-sidebar”.

      Regarding the custom plugin vs functions.php, check out this article. 🙂

      Cheers,
      Carrie

  2. This site has become a Team Member over the last few days for me. I cant thank you enough for the information. Really great articles!!!

  3. I’m having this same issue with the Primary Sidebar content showing below the dynamic sidebar I set up with Genesis Simple Sidebars.

    I used the code shown in the tutorial above and included both of these lines:

    remove_action( ‘genesis_sidebar’, ‘genesis_do_sidebar’ );
    remove_action( ‘genesis_sidebar’, ‘ss_do_sidebar’ );

    However, its still showing the Primary Sidebar content. Has anyone else seen and solved this?

  4. Is it a live site I can look at?

    I don’t see anything in the code that would interfere with the code in this tut. If your site’s not live, feel free to post your functions.php on pastebin.com.

  5. You’ve commented this line out in your code:

    remove_action( ‘genesis_sidebar’, ‘genesis_do_sidebar’ );

    Uncomment that and it should work. You only need this line if you’re using the Genesis Simple Sidebars plugin:

    remove_action( ‘genesis_sidebar’, ‘ss_do_sidebar’ );

Leave a Comment

Your email address will not be published. Required fields are marked *

Carrie Dils uses Accessibility Checker to monitor our website's accessibility.