0

In Woocommerce, I have around 6 categories and there are many subcategories in each parent category. Mostly, the description for the subcategory is the same as the parent category except for a few subcategories.

I want to show the description of the parent category automatically when there is no description available for the subcategory. When there is no description available for the parent category, just leave it blank.

Below is the code I am using in archive-product.php to display the category description

<?php
//show category description
$term_object = get_queried_object();
 ?>
<div class="woocommerce-category-description" style="margin-bottom: 40px;">
<div class="title"><h2><?php echo $term_object->name; ?></h2></div>
<div class="description"><?php echo $term_object->description; ?></div>
</div>

<?php
// Show parent category description if sub-category description is empty 
if (!category_description($category_id)) {

}
?>

I can show static description when there is no description, but I am unable to fetch the parent category description.

Thanks.

Edit: I tried below code, but it is randomly fetching parent ID instead of correct ID.

<?php
//show category description
$term_object = get_queried_object();
?>
<div class="woocommerce-category-description" style="margin-bottom: 40px;">
<div class="title"><h2><?php echo $term_object->name; ?></h2></div>
<div class="description"><?php echo $term_object->description; ?></div>
<?php
                        if (!category_description($category_id)) {
$cat = get_the_terms( $product->ID, 'product_cat' );

foreach ($cat as $categoria) {
if($categoria->parent == 0){
   $parentdesc = $categoria->term_id;
   echo category_description( $parentdesc );
}
}
}
?>
</div>
2
  • Is this code showing anything for a description? The standard WordPress template hierarchy would use archive-product.php as a post type archive for the product custom post type and so get_queried_object() would return the product object, not a term object. – Andy Macaulay-Brook Mar 26 at 10:56
  • Yes, whatever i add in WP Admin -> Products -> Categories -> Description is getting displayed in product category page front end – Avi88 Mar 26 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.