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 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!

Carrie Dils

I’m a recommended Genesis Developer with 15+ years experience in web design and development. I'm creative, resourceful, and ready to put my mind to your project. Want to discuss your WordPress project? Let's talk!

Comments

    • says

      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

    • says

      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!

    • says

      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’ );

  1. 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!

  2. says

    Hi Carrie… thanks for this post – saved me from using one more add-on than I needed to (was using widget logic before to determine what was displayed in a single sidebar depending on the page… it was a nightmare to maintain!) This should be much easier :)

  3. Hernán says

    Hi Carrie! Great post. I’ve been looking for this for a while. I have a question though. Is there a set of guidelines on when to use an archive page (archive-$customCPT.php) vs. a template page (say template-$customCPT.php). Why didn’t you want to create a custom post template in the first place? I’m curious because I’m going to start coding a website and would like to know what’s the best approach. Thanks!

    • says

      Hi there,
      It really just depends on your objective. If you use a template-$whatever.php, you’ll need to create an actual post or page and set it to use that template.

      On the other hand, if you use archive-$whatever.php, it will automatically be applied to that archive page (no “page” creation necessary). I know the terminology sounds kind of confusion. This codex page is terrible, but it might shed some light for you -> http://codex.wordpress.org/Template_Hierarchy.

      In the end, either way is appropriate, it just depends what you’re wanting to accomplish.

      Carrie

  4. says

    Hi Carrie,

    Thanks for the great tutorial. I was just wondering if you’ll be posting the other tutorial you teased at the end – about how to use WP Type and Views to pull custom fields into the sidebar – anytime soon?

    • says

      Hi Ben,
      I’m clearly running behind. :) I still intend to post it, just haven’t had a chance to write it yet. You might check over at SureFireWebServices.com – Jonathan, who runs the site, is a big T&V user. Maybe he has something helpful in the mean time!

      Cheers,
      Carrie

    • says

      This is what I’ve tried to apply a sidebar by author.

      //Add Custom Sidebar to Posts by Author
      add_action(‘get_header’,’cd_change_genesis_sidebar’);
      function cd_change_genesis_sidebar() {
      if ( is_single()) {
      global $post;
      $author_id=$post->post_author;
      if ($author_id == ‘Author’); {
      remove_action( ‘genesis_sidebar’, ‘ss_do_sidebar’ );
      add_action( ‘genesis_sidebar’, ‘cd_do_sidebar’ );
      } }
      }

      //Function to output my custom sidebar
      function cd_do_sidebar() {
      dynamic_sidebar( ‘Author’ );

      }

      Not quite there. It’s applying the same sidebar to all posts. Any ideas?

  5. says

    Carrie, once again one of your tutorials saved me hours of ridiculousness. My client love the Metro Pro theme but wants a different sidebar on the front page than the rest of the site. I was feeling like a plugin would be overkill in this situation, so I used your code on the front-page.php file (except, of course the “register sidebar”). The only thing I did differently was remove: [code]if ( is_singular(‘jobs’)) { // Check if we’re on a single post for my CPT called “jobs”[/code] because I’m not looking for a CPT in this case. Worked beautifully. Again, thanks so much – please, Code Goddess, keep it up!!!

  6. kat2013 says

    Hi Carrie, Great post. This is exactly what i need. I too am using Types & Views, but I am not using Genesis, I am creating my own theme from scratch (based on the _s theme) and am not proficient in PHP at all. Do you know how i would modify this code in order that it would work in my theme?

    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’ );
    }

    • says

      Hi,
      The only thing Genesis-specific about that code is ‘genesis_sidebar’ and ‘genesis_do_sidebar’. Genesis child themes don’t use sidebar.php (it pulls from the parent framework), so I’m not sure what best practice would be when it comes to underscores. You can likely just edit sidebar.php directly to display content conditionally.

      I’d skip this tut and look for better examples of conditional or custom sidebars. :)

      Carrie

      • Kat2013 says

        Ah OK. I’ll see if I can decipher that :) I am using the Custom Sidebars plugin which si great for indivdual pages, but not for entire post types. I’m hoping to find a solution that means i don’t have to create a custom template for each post type. I’ll keep searching :)

  7. says

    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’ );
    }

    • says

      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?

      • says

        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’ );
        }

  8. Mark Law says

    Thanks so much for posting this, I now have a conditional custom widget area for clients site, thanks to you! Just a note though for people using Genesis Simple Sidebars “Genesis Simple Sidebars unhooks the regular Genesis genesis_do_sidebar action and replaces it with it’s own” so replace genesis_do_sidebar with ss_do_sidebar

    Thanks to this chap http://www.studiopress.com/forums/topic/remove-sidebars-via-action-hook for discovering this :)

Trackbacks