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 am looking for a way to do some conditional logic on terms associated with a post.

Essentially I created my own custom taxonomy for "age groups" and have created three terms for them. Kids, Teens, Adults... In the admin area I want to check the terms which apply to a specific post and on the frontend of the site within my page template I want to show a specific image if the term was associated with the post or a different one if the term was not associated.

Does anyone have a solution for this... I thought the following code example would work but it does not. (BTW - what I am doing here is changing the image based off css).

<li id="kids-<?php if ( is_term( 'Kids' , 'age_groups' ) ) {
   echo 'on';
} else {echo 'off';} ?>">Kids Programs</li>
share|improve this question

1 Answer 1

up vote 6 down vote accepted

Hi @NetConstructor:

First thing, assuming your logic worked you can use the ternary operator to simplify your example:

<li id="kids-<?php echo is_term('Kids','age_groups') 
   ? 'on' : 'off'; ?>">Kids Programs</li>

The issue seems to be that is_term() is used to check if a term exists, not if it is associated with a particular post. I think what you really want is is_object_in_term() (which assumes that you are in The Loop, i.e. that $post has an appropriate value):

<li id="kids-<?php echo is_object_in_term($post->ID,'age_groups','Kids') 
   ? 'on' : 'off'; ?>">Kids Programs</li>

P.S. Assuming is_term() had been the right function, it has actually been deprecated; term_exists() replaces is_term(); just fyi.

share|improve this answer
    
this worked perfectly. Thanks!.... I knew there must have been a simple solution for what I was looking for. –  NetConstructor.com Oct 25 '10 at 6:55
    
@NetConstructor - Glad it helped! –  MikeSchinkel Oct 25 '10 at 7:44

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.