1

This code displays the category thumbnail and title of the two parent categories for WooCommerce product categories.

I have several child categories I'd like to display as well. Is there way way to show all categories OR select which category IDs to display?

$taxonomyName = "product_cat";
$prod_categories = get_terms( $taxonomyName, array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => -1
) );  
foreach( $prod_categories as $prod_cat ) :
    if ( $prod_cat->parent != 0 )
        continue;
    $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
    $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
    $term_link = get_term_link( $prod_cat, 'product_cat' );
    ?><img  src="<?php echo $cat_thumb_url; ?>" alt="" /> 
    <a class="button" href="<?php echo $term_link; ?>"> <?php echo $prod_cat->name; ?> </a>
<?php endforeach; 
wp_reset_query();

1 Answer 1

3

Here is the solution for your Question: Please check this one, I hope it will help you to display child category details.

  $taxonomy     = 'product_cat';
  $orderby      = 'name';
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no
  $title        = '';
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                 echo  $sub_category->name ;
                 $thumbnail_id = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
                 $image = wp_get_attachment_url( $thumbnail_id );
                 echo  '<img src="'.$image.'" alt="" height="20" width="20">';
                //add other code here to display child details

            }   
        }
    }       
}
3
  • Thanks for your reply. That code works and does show all of the categories but it doesn't display the thumbnail. I'll try to get it working. Commented Dec 3, 2015 at 19:16
  • oKay I am updating answer again for get thumbnail image. Thanks Commented Dec 3, 2015 at 19:51
  • Where's this code to must be put in? function.php of theme? Or?
    – panjianom
    Commented Jun 27, 2021 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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