WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the code I use to display all product categories with thumbnails:

<?php
$cats = get_terms('product_cat', array(
    'hide_empty' => 0,
    'orderby' => 'name'
));
?>
<div class="container"> 
    <?php foreach($cats as $cat) : ?>           
        <?php   global $wp_query;
            if($cat->category_parent == 0) {
                $category_id = $cat->term_id;
                $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                $image = wp_get_attachment_url( $thumbnail_id );
                echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
                echo '<img src="'.$image.'" width="45px" height="45px" />';
            }
        ?>      
    <?php endforeach; ?>
</div>

Could anyone help me how to split the categories of subcategories, while having only thumbnails subcategories eg. like this:

  • category (text)

    • subcategory (thumbnails)
    • subcategory (thumbnails)
  • category (text)

    • subcategory (thumbnails)
    • subcategory (thumbnails)
share|improve this question
up vote 1 down vote accepted

I found how to make this:

<?php
$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
);
?>

<?php $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {  

    if($cat->category_parent == 0) {?>


<?php   $category_id = $cat->term_id;
        $thumbnail_id   = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        echo '<h3 style="font-size:16px">'. $cat->name .'</h3>';


        $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) {
        if($sub_cats->$sub_category == 0) {
            $thumbnail_id   = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );

            echo '<li style="list-style-type: none"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="'.$image.'" width="45px" height="45px" style="margin-right:8px; margin-top:8px; float:left; " /></a></li>';
            }
        }
        echo '</li>';
        }?>

<?php   } 
 } ?>
share|improve this answer

protected by kaiser Jan 27 '15 at 16:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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