Search code examples
functiontaxonomy

Function to show if taxonomy term is used in post


I am trying to verify that I used a specific taxonomy term in specific posts. I want to create a function that will show me if that term is used by displaying yes or no.

Right now I have this, but it's not working:

function qc() {
    echo 'Sundry is used in post — ';
if ($mytax_terms = get_term( 'Sundry', $post->ID, 'mytax'));
    echo 'Yes';
}

add_shortcode( 'qc_display', 'qc' );

Right now, it simply says yes on every post.

I started out with this:

function qc() {
    echo 'Sundry is used in post — ';
$mytax_terms = get_the_term_list( $post->ID, 'mytax');
    echo $mytax_terms;
}

add_shortcode( 'qc_display', 'qc' );

Obviously that showed all the terms in the custom taxonomy, so it wasn't too helpful.

Can you please help me?

Thanks.


Solution

  • got it. just swapped out if ($mytax_terms = has_term( 'sundry' , 'mytax' ) ) { for if ($is_category = has_term( 'sundry' , 'category' ) ) {. That seemed to do the trick.