How to get current taxonomy term from my custom taxonomy array in WordPress post & echo it?

How to get/print/echo the current assigned term on the WordPress post by checking from my custom taxonomy array & echo that taxonomy term [not just its name but a clickable hyperlink term] ?

I searched through google a lot , didnt find answer to my problem, previously also asked a question HERE but only reached halfway to the answer

Here is the code :

if( has_term( [ ‘laptop’, ‘TV’, ‘phone’, ‘tablet’ ], ‘product_type’ ) ) {
//* Get the terms
$terms = get_terms( [
‘taxonomy’ => ‘product_type’,
‘slug’ => [ ‘laptop’, ‘TV’, ‘phone’, ‘tablet’ ],
] );
//* There could still be up to 4 terms, so use the first
echo $terms[0]->name;
}

This code checks my custom post type products for those terms laptop TV phone tablet which belong to the custom taxonomy product_type & then simply echoes the 1st term

I think the problem is here echo $terms[0] as it echoes the 1st created term not the actual term which is assigned to the wordpress custom post products

Just to confirm it, I tried changing values like echo $terms[10] echo $terms[20] & noticed that it echoes term which are not even present in the above arrayfor get_terms . Note the number 10 & 20 matched with the terms present at position 10 & 20 in wp-admin’s custom taxonomy list page

I hope someone can solve this problem

Read more here:: How to get current taxonomy term from my custom taxonomy array in WordPress post & echo it?

Leave a Reply

Your email address will not be published. Required fields are marked *