Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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));
share|improve this answer
    
I did find Widget Logic through another post and tried it, but found the documentation lacking and couldn't get it to work. –  Drai Sep 23 '11 at 17:20
    
humm... works gret for me. What's your experiance level with PHP? Also, did you try it with the snippit I posted above? –  BandonRandon Sep 23 '11 at 18:49
    
I tried it with is_single( '96' ) id of post. I am not adept with PHP, and that didn't work. I don't get what to do with the last suggestion about the post type. –  Drai Sep 24 '11 at 5:06
    
Humm.. works like a charm for me. It could be a theme related issue though. For example if your theme is overwriting your widgets. –  BandonRandon Sep 24 '11 at 7:00
1  
To be sure that there is no theme problem try Widget Logic approach on default theme (Twenty Eleven). –  Mamaduka Sep 24 '11 at 8:26

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.

share|improve this answer
    
I already registered the sidebars, so I think your approach is what I was looking for. Let you know how it works. –  Drai Sep 23 '11 at 17:21
    
Chip, which part of this is telling Wp to show the sidebar for the particular post? When you refer to replace {post-type} with the actual name/slug are you talking about the post type itself or the individual post slug? –  Drai Sep 24 '11 at 5:24
    
@Drai Chip's method only will display different sidebar for your CPT –  Mamaduka Sep 24 '11 at 8:24
    
What did you name the Sidebar for your CPT? Use that in place of sidebar-{post_type}. –  Chip Bennett Sep 24 '11 at 13:16
1  
@Mr.Brown in that case, just use Widget Logic. –  Chip Bennett May 16 '12 at 19:39

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!

share|improve this answer
    
You don't NEED a plugin for anything really. However, plugins often make things way simpler. I COULD add 20 lines or so to my themes functions.php OR just install a plugin and let it do the work saving myself time and a headache. –  BandonRandon Sep 23 '11 at 16:16
    
interesting. I think I prefer the switching sidebar solution, but good to know. –  Drai Sep 24 '11 at 4:38
    
@Drai Sure. I believe in having a total control over my code (and if the task is easy), hence I used the filter. Anyways, good luck with whatever works for you! :) –  Rutwick Gangurde Sep 24 '11 at 4:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.