Woocommerce category description

2019-08-28 03:39发布

问题:

I have next code:

 <?php 
 global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = get_the_terms($category->slug,'product_cat', $args);

    $count = count($terms); 
    if ($count > 0) {

        foreach ($terms as $term) {
            echo '<div style="direction:rtl;">';
            echo $term->description;
            echo '</div>';

        }

    }

?>

The code will display category description. The problem - on sub-category it will display the sub-category description + the parent description.

How i can display the description separate: in parent - the parent description, and on sub - only the sub description?

回答1:

The answer:

 <?php 
 global $post;
$terms = get_the_terms( 'product_cat',$post->ID);


            echo '<div style="direction:rtl;">';
            echo category_description( get_category_by_slug($terms)->term_id);
            echo '</div>';

?>


回答2:

You can use this code to display the product category description -

<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>


回答3:

Try this and let me know if it helped you

add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description', 
12);
function custom_add_product_description ($category) {
$cat_id        =    $category->term_id;
$prod_term    =    get_term($cat_id,'product_cat');
$description=    $prod_term->description;

echo '<div>'.$description.'</div>';
}