Mr.Mullet
Mr.Mullet

Reputation: 11

Wordpress show different sidebar when one of two conditions is met (is_category('25')) II (in_category('25'))

I want to display different sidebars in wordpress for different categories and for posts belonging to these categories. I want to make this happen within one expression.

<?php if (is_category('25')) : ?>
<p>Sidebar for Category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>

This works but when I try to display the custom sidebar for category page and single posts from cat25 at the same time with:

<?php if (is_category('25')) || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>

nothing happens.

I have a learning deficit and a hard time with logic. But still I try and I keep improving.

Please consider this when giving me an answer.

Upvotes: 0

Views: 33

Answers (1)

Mr.Mullet
Mr.Mullet

Reputation: 11

I made a simple mistake. I closed to early with the ) and used the ) one more time at the end.

At least this might help someone coming from a search engine.

This is the working code:

<?php if (is_category('25') || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>

If someone has a simpler solution or any changes according to best practices please still feel free to give your input :-)

Upvotes: 1

Related Questions