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. Carrie:

    I’m running on Genesis and the Dynamik Website Builder (DWB) child theme. I have a CPT called Vignettes (the singular name is “vignette”) and I’ve created a Vignette Sidebar (id = vignettes-sidebar) using Genesis Easy Sidebars.

    I want to apply my new sidebar to my CPT and I’ve added the following code (from your post) to my DWB Custom Functions metabox, but it’s not working. Let me know if you see anything wrong.

    /* CHANGE SIDEBAR FOR VIGNETTE CPT using code by Carrie Dils */

    add_action(‘get_header’,’cd_change_genesis_sidebar’);
    function cd_change_genesis_sidebar() {
    if ( is_singular(‘vignette’)) { // Check if we’re on a single post for my CPT called “Vignette”
    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( ‘vignettes-sidebar’ );
    }

    1. Please ignore my request above. I just realized that it’s working. But what I really want to make happen is to add my new Vignettes Sidebar to my Vignette CPT “Archives” … do you know of any code that will make this happen? Perhaps a modification of the above code so that it is the archive sidebars that are switched out?

      1. I tried the following based on comment by Travis Smith (above), but it caused both admin and my website to crash, so I had to remove it from Custom Functions via FTP:

        /* CHANGE SIDEBAR FOR VIGNETTE CPT ARCHIVE using code by Carrie Dils */

        add_action(‘get_header’,’cd_change_genesis_sidebar’);
        function cd_change_genesis_sidebar() {
        if ( is_post_type_archive( ‘vignette’ )) { // Check if we’re on a single post for my CPT called “Vignette”
        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( ‘vignettes-sidebar’ );
        }

        1. Hi Lang,

          Check out the comments from this post. Sidebars created by the Genesis Simple Sidebars plugin are created/removed with a different function that regular Genesis sidebars. The syntax change there should do the trick for you.

          Cheers,
          Carrie

        2. Hi Carrie, you should put a disclaimer in this post about using Genesis extender and checking the box Custom Functions Effect Admin. I checked this as I was following your how-to, but didn’t realize how easily it could bring the website down. I put some bad php code into custom functions, and then was unable to log into WordPress to correct it. I think it’s better to leave this setting in extender unchecked, especially when you are in development and constantly updating the custom functions in the Extender UI. Anyway, thought I would share this with you. Otherwise, this post was extremely helpful to me. I appreciate such a thorough how-to on the subject of sidebars, since I am new to Genesis and Extender both. The comments on this post are also amazing. So much to learn, thanks!

  2. Pingback: Using Genesis Simple Sidebars Plugin To Add New Sidebar To Custom Taxonomy Archive Page

  3. Hey Carrie,

    I’m using simple sidebars and am having trouble removing the default sidebar. I’ve tried:
    remove_action( ‘genesis_sidebar’, ‘ss_do_sidebar’ );
    remove_action( ‘genesis_sidebar’, ‘genesis_do_sidebar’ );
    but neither seems to work. Do you know if this has changed since 2.0?

    Thanks!

  4. Hi Carrie!

    Hoping you could help out. Myself and another Genesis user have asked the same question in the Genesis forum, however it looks like we are both stumped. How do we target the css for each custom sidebar created. In example we have a custom sidebar for “services” how could we target just that sidebar to say add a border?

    I did use firebug, but the css is targeting the primary sidebar: .sidebar.sidebar-primary.widget-area.

    Obviously we could create a rule manually that only targets the css with the above code for specific pages, but then every time we add a page that has that sidebar we would have to add to the rule, too much work, for what seems to have an easier solution. As the ID of the custom sidebar is NOT for css, is there a way to add a custom css class to each sidebar created?

    If you could lend a hand that would be awesome!

    Thanks
    Doug

  5. For any of you who have Genesis Simple Sidebars installed and are trying to switch sidebars on a CPT archive, or if you have WooCommerce installed, you should know that each of these plugins adds their own action to add a different default sidebar.

    So here’s some sample code to remove the default sidebar with either/both of these installed:

    remove_action( ‘genesis_sidebar’, ‘genesis_do_sidebar’); // remove the default genesis sidebar
    remove_action( ‘genesis_sidebar’, ‘gencwooc_ss_do_sidebar’ ); // remove the woocommerce sidebar
    remove_action( ‘genesis_sidebar’, ‘ss_do_sidebar’ ); // remove the default sidebar added by genesis simple sidebar

    Here’s a more full code sample, assigning two sidebars in different places on the site and removing all of the other ones from those places:
    https://gist.github.com/redblueconcepts/569876a43c034bd67852

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.