1

Hi I want to display all the categories of products in a loop to display them in category menu along the numbers of products each category contains. Some thing like that

enter image description here

so far I have done this to get all categories

 $args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) { echo $cat->name; }

But I want to know how to display products numbers in each category.

3

You just need to add $cat->count to get the count of all products in that category. Hope this helps you out.

$args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

$product_categories = get_terms( 'product_cat', $args );

foreach( $product_categories as $cat )  { 
   echo $cat->name.' ('.$cat->count.')'; 
}
-1
Category
                        <ul class="Category-list">

                            <?php

            $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' =>0));
            $count = $category->category_count;

            foreach($wcatTerms as $wcatTerm) : ?>
            <?php
            $thumb_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
            $term_img = wp_get_attachment_url(  $thumb_id );
            ?>
            <li><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?>(<?php echo $wcatTerm->count;?>)</a></li>
                <?php endforeach;  ?>
                        </ul>
                    </div>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.