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.
Leave a Reply
You must be logged in to post a comment.
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
You could also try the a post type method to show only a specific post type:
A separate approach would be to register multiple sidebars, and then call each one conditionally. e.g.
Register sidebars:
(Note: replace
{post-type}
with the actual name/slug of your CPT, as appropriate.)Then, in your template file:
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
andsingle-{post-type}.php
, then you can simply calldynamic_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
andsingle.php
to display your CPTs, then use the conditional code I originally suggested.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:
and replace it with
That’s it!
Hope it helps!