How do I determine if a certain term is in an array?

I am currently running the below wordpress php and it is creating a list for me.

$term = get_term_by( 'id', $ptc, $taxonomy );
echo '<li><a href="'. home_url() .'/taxon/'. $pt->slug .'/' .  $term->slug . '">' . $term->name . '</a> </li>';

However, I need it to check to see if $term is in_array $product_terms so I tried:

Read More
$term = get_term_by( 'id', $ptc, $taxonomy );
                if(in_array($term, $product_terms)){ 
                    echo '<li class="current-cat"><a href="'. home_url() .'/taxon/'. $pt->slug .'/' .  $term->slug . '">' . $term->name . '</a> </li>';
                } else {
                    echo '<li><a href="'. home_url() .'/taxon/'. $pt->slug .'/' .  $term->slug . '">' . $term->name . '</a> </li>';
                }

But it still returns none with the extra class even though I know one of them is in the array… Any ideas how to make this work?

Related posts

Leave a Reply

1 comment

  1. It depends on what kind of type are the values from your $product_terms array.

    If you have strings there, like term slugs, then you probably want to check if $term->slug exists.

    If you have objects, then make sure the array is indexed, and not associative. For associative arrays use array_key_exists() instead.