0
votes

Trying to work out how you would go about checking if a Woocommerce category is the parent category.

I have a category and a sub category and in my Woocommerce template, have 2 separate layout HTML. 1 for the parent category and 1 for the sub category.

I'm currently using this:

if ( is_product_category( 'main-category' )  ) { ?>

But would be easier if it could check without manually inputting all parent categories.

And also, is there a way to link to the main parent category page from the sub category or sub categories product page?

1

1 Answers

0
votes

How to get Parent Category with total Subcategory count?

<?php
$category_object        = get_queried_object();
$category_taxonomy      = $category_object->taxonomy;
$category_term_id       = $category_object->term_id;
$category_name          = $category_object->name;
$category_description   = $category_object->description;
$category_slug          = $category_object->slug;
$category_children      = get_terms( $category_taxonomy, array(
    'parent'    => $category_term_id,
    'hide_empty' => false
    ) );
$category_children_count = count($category_children);   
if($category_children_count>0)
{
    //get Parent Category with Subcategory count
}
?>