0

I have a custom post type 'speakers' and I have a checkbox custom field (meta_query) to separate my Conference speakers from my Workshop speakers.

I can filter my speakers with this meta_query and things are working fine. But how do I use a different sidebar based on the Conference or Workshop speaker meta_query?

When I click the speakers for their detail (no matter Conference or Workshop speaker) the detail page loads my single-speakers.php and the code to get the sidebar in my single-speakers.php is:

<?php get_sidebar(); ?>

How can I specify a different sidebar, for example get_sidebar('workshop')? Can I write a conditional code to load a different sidebar based on the speakers meta_query? Does this make sense?

Thanks

2 Answers 2

1

First off - it sounds like you might actually be better separating your speaker types by using a taxonomy.

Taxonomies are for grouping things together and the query that WordPress runs, for example: to get conference speaker is faster than doing it by meta query.

Also you have the added bonus of being about to use taxonomy archive which means it's easier to write conditionals.

So for this example I would have a taxonomy called 'speaker_type'. You can then use the template taxonomy-speaker_type.php to style the output (this might be roughly the same as you're using for your post archive archive-speakers.php)

You can then use the conditional in your sidebar:

is_tax( 'speaker_type', 'conference' );

Hope that helps.

1
  • Steven, you're absolutely right. Right after I added the custom field and changed all my post types, I figured taxonomies would be the way to go not custom fields. I'll keep that in mind.
    – hnnnng
    Commented Mar 8, 2014 at 17:50
1

If you end up with the type in a variable $answer ...

if($answer == "conference") {
    get_sidebar('conference');
} else {
    get_sidebar('workshop');
}

or perhaps:

get_sidebar($answer);
1
  • This looks like a different approach and promising. I'll give it a shot. Thanks.
    – hnnnng
    Commented Mar 8, 2014 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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