Question

Struggling to find out how to achieve this, if it is at all possible. The equivalent of is_category but for a custom taxonomy I've set up. The taxonomy category if which being Taxonomy Name with 2 taxonomies: Taxonomy One and Taxonomy Two. But this:

<?php if ( has_term('taxonomy-one' ) ) {?>
    <div class="test" style="color: red"><?php the_field('field_name'); ?></div>
<?php } elseif ( has_term('taxonomy-two' ) ) {?>
    <div class="test" style="color: green"><?php the_field('field_name'); ?></div>
<?php } ?>

There doesn't seem to be any documentation for this either so I'm not at all sure if it's possible, I'm hoping so though, any suggestions would be greatly appreciated!

Était-ce utile?

La solution

I'm pretty sure you shouldn't use negation in your conditions... If you check ! is_tax... it will be true not only for other taxonomy pages, but also for singular pages, and any other...

So it should look like this:

<?php if ( is_tax('taxonomy-name','taxonomy-one' ) ) {?>
    <div class="test" style="color: red"><?php the_field('field_name'); ?></div>
<?php } elseif ( is_tax('taxonomy-name','taxonomy-two' ) ) {?>
    <div class="test" style="color: green"><?php the_field('field_name'); ?></div>
<?php } ?>

And about lack of documentation... I have no idea where have you looked for, but there is codex page for is_tax: http://codex.wordpress.org/Function_Reference/is_tax ;)

PS. If you want to check if given post is assigned to term, then you should use has_term function.

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top