Conditional if is single and part of the custom taxonomy

admin2025-01-07  6

I want to write code only if it is single and part of the custom taxonomy. Here's how my custom taxonomy edit URL looks like:

.php?taxonomy=us_pf_category&tag_ID=53&post_type=us_p

I tried many different ways but not working. Here's one example:

if ( is_single() && has_term( ' ', '53' ) )

What's wrong going on? For the regular category, it's working with the in_category function. I don't know how things work for custom taxonomy type. How can I achieve this?

I want to write code only if it is single and part of the custom taxonomy. Here's how my custom taxonomy edit URL looks like:

http://example.com/wp-admin/term.php?taxonomy=us_pf_category&tag_ID=53&post_type=us_p

I tried many different ways but not working. Here's one example:

if ( is_single() && has_term( ' ', '53' ) )

What's wrong going on? For the regular category, it's working with the in_category function. I don't know how things work for custom taxonomy type. How can I achieve this?

Share Improve this question asked Dec 17, 2020 at 9:06 akarimakarim 3052 gold badges5 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The first problem is that you've got a space in the first argument of has_term(). If you want to check for any term then you need to pass an empty string, but a space is not an empty string. Then, as documented, the second argument needs to be the taxonomy name.

This should work:

if ( is_single() && has_term( '', 'us_pf_category' ) )
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254587a218.html

最新回复(0)