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 ...
}
}
Related Posts:
- Send an email when custom post type category is changed
- what is the action hook code to supporting product category condition in single product page of woocommerce?
- Prevent function from triggering again when post in specific category gets another category?
- Action hooks returning old category instead of new category
- Get term by slug in Gutenberg
- How to remove the “+Add Category” button from the Category metabox?
- Hide posts of a certain category unless logged in
- Add title, post content, and category like add_post_meta and update_post_meta
- wp_dropdown_categories() scripts working erratically on sidebar
- REST API: Display Category names in JSON?
- wp_dropdown_categories not showing option as selected
- How to access deleted term inside delete_product_cat action
- Filter WooCommerce archive pages by an additional category
- javascripts files not linking to my category slugs?
- How to load several hierarchical categories from functions.php
- create function to call category name and slug
- ADD DYNAMIC attribute for category to accordion
- Get_term_meta() does not work with pre_get_posts()
- How to check a category checkbox using JavaScript
- Modify functions.php to add a term ‘uses-theme’ set to theme name on post save
- Javascript code for Category
- Can’t filter categories using isotope
- How to create an automatic MultiColoumn MegaMenu with Categories WordPress
- Is There a Difference Between Taxonomies and Categories?
- List all subcategories from category
- How To Find Out WordPress Category Table in MYSQL?
- Add custom field to Category
- How to enqueue scripts on custom post add/edit pages?
- Get Category ID inside Category template
- Only one category per post
- WordPress Multisite – global categories
- Get the children of the parent category
- Get posts from sites in Multisite?
- WP REST API: filter by category 1 AND category 2
- How To Get Parent Category Slug of Current Post
- Display All Products by Category with WooCommerce
- What is the difference between a “tag” and a “category”?
- Is there an easy way to make a meta box have the tabs like the Categories meta box has?
- How to check if I’m on the last page of posts?
- Display posts from the same category using next/previous post link
- How would I get a taxonomy/category list inside a Gutenberg block?
- Can I set a default featured image for a category?
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- Force category choice before creating new post?
- Gutenberg editor add a custom category as wrapper for custom blocks
- WooCommerce: List All Categories
- How to get category and archive title?
- Get category ID from category slug (not working)
- How do I remove “Uncategorized” from posts with more than 1 category?
- Get rid of WordPress category, tag and author archives?
- Individual rss feed entry length for categories?
- How can I create a category landing page followed by pages of posts?
- Remove parent category from permalink? Basically only have the child category?
- Custom permalink structure leads to be 404 on pagination
- Retrieve Product category ‘NAME’ by product category ID – WooCommerce?
- Posts URL structure like site.com/category/the-post-title
- Can’t custom taxonomies have same terms (slugs) as categories?
- How to get permalinks with category base working with sub-categories
- How to remove the term “category” from category pagination?
- Using wp_category_checklist in a widget
- WP 3.9 TinyMCE no longer loads on category description editor
- On the category page, get the category object
- Get only the top level categories using get_categories() without foreach loop
- Get all categories and posts in those categories
- How to add featured image for category (without a plugin)?
- How to create a custom sort for WordPress categories
- How to display non-hierarchical taxonomy as terms with checkboxes?
- Category archive by year with permalink support /category/YYYY
- Best Way to Redirect Category to Page and Hide Category
- get_terms vs. get_categories: does it matter?
- What is a good robots.txt?
- Allow users to create their own feed from selected categories?
- Hook when category is added to post
- get_categories for custom post type with a specific custom taxonomy attached
- Add a URL prefix to permalinks of one category of posts only
- How do you add categories to custom post types in WordPress?
- Create a category list page
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Woocommerce get category image full size
- exclude category from get_posts?
- How To Modify The Loop in archives.php To Have 11 Posts Per Page and CSS Styling
- How can I hide a category from Contributors in the edit/add new post screen?
- Order get_terms() By Custom Field
- WordPress Multiple Category Search
- Should category.php and The Loop be used if the query needs to be customizable?
- How do the ‘tag’ and ‘category’ (default) taxonomies do ‘save_post’ action?
- How to Change the Categories Order in the Admin Dashboard?
- Control term order on a per-post basis
- How to remove categories filter from wordpress admin?
- get all tags from category
- Could not insert term into the database
- How can I add the featured image functionality to a custom taxonomy?
- Activate Gutenberg in category-descriptions
- Filter by post category in Media Library
- Filtering categories in the permalink structure
- Share one taxonomy across multiple blogs in 3.0 [duplicate]
- 404 Error On Category and Tags Pages
- How to show subcategories in categories else show posts
- Allow duplicate permalink slugs for posts in different categories