change sidebar or just widgets for 2 specific post type single posts

I would like to show a different sidebar for 2 specific posts(same custom post type). I understand how to do this using separate templates for different pages, but I want to have it all happen in the single-posttype.php page. Or am I thinking about this wrong? My end goal is to show different text widgets in the sidebar on specific posts.

Related posts

Leave a Reply

3 comments

  1. I think Widget Logic is what you’re looking for. It adds a field to each widget to allow you to specify which post get which widgets. It uses the standard Conditional Tags making it easy to use. You’d just want to do something like

    is_single( 'Slug1-or-ID1' ) ||  is_single( 'Slug2-or-ID2' )
    

    You could also try the a post type method to show only a specific post type:

    //not tested but something like     
    global $post; return ('book' == get_post_type($post));
    
  2. A separate approach would be to register multiple sidebars, and then call each one conditionally. e.g.

    Register sidebars:

    // Registers Primary Widget Area
    register_sidebar(
        array (
            'name' => 'Sidebar',
            'id' => 'sidebar',
            'description' => __'Primary sidebar widget area',
            'before_widget' => '<div class="widget">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
        ) 
    );
    // Registers Primary Widget Area
    register_sidebar(
        array (
            'name' => '{Post Type} Sidebar',
            'id' => 'sidebar-{post-type}',
            'description' => __'{Post Type} sidebar widget area',
            'before_widget' => '<div class="widget">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
        ) 
    );
    

    (Note: replace {post-type} with the actual name/slug of your CPT, as appropriate.)

    Then, in your template file:

    $sidebar_id = ( 'custom-post-type-slug' == get_post_type() ? 'sidebar-{post-type}' : 'sidebar' );
    
    dynamic_sidebar ( $sidebar_id );
    

    So, you won’t need to create separate template files for your CPT; however, you will have two separate Widget areas appear in Dashboard -> Appearance -> Widgets, that you will have to populate separately.

    EDIT

    How are you displaying your CPTs?

    If you’re using archive-{post-type}.php and single-{post-type}.php, then you can simply call dynamic_sidebar( 'sidebar-{post-type}' ) (or whatever you named it when you registered it) in those template files.

    Otherwise, if you’re using the normal archive.php and single.php to display your CPTs, then use the conditional code I originally suggested.

  3. You don’t need a plugin for this! There’s a filter called ‘sidebars_widgets’ defined in one of the core files, which will allow you to achieve this very easily! I have written a simple tutorial on how to do it. Show/hide widgets on specific pages. Check the example there, look for the line:

    if(is_front_page() || is_home())
    

    and replace it with

    if(is_singular('posttype'))
    

    That’s it!
    Hope it helps!