Support » Themes and Templates » How to toggle a css class based on Taxonomy

  • ekdor

    (@ekdor)


    I am not proficient with php so could someone please help me. I want to be able to have a div element to check if a particular taxonomy is selected or not and choose between two classes based on the results.

    This is the code I already use to insert an image, which I want to replace:
    <?php $terms = get_the_terms($post->ID, 'status'); foreach( $terms as $term) { echo '<IMG CLASS="statusPos-list" SRC="' . get_bloginfo('template_url') . '/images/status-' . $term->slug . '.png" ALT="*" />'; break; } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • KrissieV

    (@krissiev)

    try something like this

    <?php $terms = get_the_terms($post->ID, 'status'); foreach( $terms as $term) {
    if ($term->slug=='the-term-slug'){
    echo '<IMG CLASS="statusPos-list" SRC="' . get_bloginfo('template_url') . '/images/status-' . $term->slug . '.png" ALT="*" />'; break;
    } else  {
    echo '<IMG CLASS="statusPos-list additional-class" SRC="' . get_bloginfo('template_url') . '/images/status-' . $term->slug . '.png" ALT="*" />'; break;
    }; ?>
    ekdor

    (@ekdor)

    Thanks for your reply.

    Hmm, I just realised I failed to ask my question clearly. The code I supplied puts an image in place of that code if a taxonomy is selected for a particular article. Works well enough as is. To help describe my intensions. I have this code right after the name of an article which is listed in a loop. So what I think I would like to try, instead of adding an image after the name, is toggle the class for the name. So for example; normally the name might be back text, but if this taxonomy is selected it uses a different class that makes the text red.

    I had come across something like this in the past while research a solution to a different issue. But for the life of me now that I want it I can’t find it.

    Regards,.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to toggle a css class based on Taxonomy’ is closed to new replies.