Target WooCommerce product category archive pages from a child category term

Rate this post

I have a question regarding the product category. I have a category like that:

-- 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 when I use it doesn’t work. Should I use the following codes or there is another option:

is_product_category( ‘laptop’ )
is_product_category( ‘mobile’ )



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:

 * @param int|object $parent ID or object term object to check.
 * @return bool Whether the product category being viewed is a child of the given parent category.
function wpse_is_child_product_category( $parent ) {
    if ( ! is_product_category() ) {
        return false;

    return term_is_ancestor_of( $parent, get_queried_object(), 'product_category' );


if ( is_product_category( 5 ) || wpse_is_child_product_category( 5 ) ) {
    // . . .

Leave a Reply

Your email address will not be published.

Back to top button