I have a question regarding the product category. I have a category like that:
-electronic
-- laptop
-- mobile
then I want to create a logic for all product under electronic
, I use is_product_category( ‘electronic’ )
, but it doesn’t work for electronic, it only works when URL is mywebsite.com/product-category/electronic
when I use mywebsite.com/product-category/electronic/mobile/
it doesn’t work. Should I use the following codes or there is another option:
is_product_category( ‘laptop’ )
is_product_category( ‘mobile’ )
2
Answers
You can use
term_is_ancestor_of()
to check if the current term (product category) being viewed belongs to a parent term.I’ve written a simple helper function:
Usage:
You can create a custom conditional function that handle also any children product category on product category archives like (handle term name, term slug or term id):
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
USAGE: