in_category()
should be used inside The Loop (unless if you pass a post ID as the second parameter like in_category( 1, 1 )
) and only for the default/built-in category
taxonomy.
For custom taxonomies like the product_cat
in your case, you should use has_term()
; however, if you’re checking if the current request/page is a taxonomy archive page, you should instead use is_tax()
:
function wpb_hook_faqtocategory_javascript() {
if ( is_tax( 'product_cat', 9 ) ) { // 9 is the term ID, but you can use the slug/name
?>
<script type="application/ld+json">
</script>
<?php
}
}
Or in WooCommerce (you’re using WooCommerce, right?), you can use is_product_category()
, but you’ll need to know the term slug:
function wpb_hook_faqtocategory_javascript() {
if ( is_product_category( 'term-slug' ) ) {
// ... your code here ...
}
}