9

I need to show all subcategories using

$product_category = wp_get_post_terms( $post->ID, 'product_cat' );

actually I use:

<?php 

global $post;

$terms = get_the_terms( $post->ID, 'product_cat', 'hide_empty=0'  );
foreach ( $terms as $term ){
    $category_id = $term->term_id;
    $category_name = $term->name;
    $category_slug = $term->slug;

    echo '<li><a href="'. get_term_link($term->slug, 'product_cat') .'">'.$category_name.'</a></li>';
}   

?>

It's ok but it shows only the parent category and only one subcategory...

How to fix that?

2 Answers 2

22

Try something like this:

by ID

function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
    $args = array(
       'hierarchical' => 1,
       'show_option_none' => '',
       'hide_empty' => 0,
       'parent' => $parent_cat_ID,
       'taxonomy' => 'product_cat'
    );
  $subcats = get_categories($args);
    echo '<ul class="wooc_sclist">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
      }
    echo '</ul>';
}

by NAME

function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {
  $IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');
  $product_cat_ID = $IDbyNAME->term_id;
    $args = array(
       'hierarchical' => 1,
       'show_option_none' => '',
       'hide_empty' => 0,
       'parent' => $product_cat_ID,
       'taxonomy' => 'product_cat'
    );
  $subcats = get_categories($args);
    echo '<ul class="wooc_sclist">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
      }
    echo '</ul>';
}

Source/inspiration

Edit:

Completed the code, tested it, see comments

13
  • Thanks, but I don't know how to put that code into mine, anyway I found this code : [wordpress.stackexchange.com/questions/73795/… but it shows All categories, I need just One category selected with its subcategories
    – Yhis
    Commented May 30, 2013 at 10:51
  • Thanks my friend, I copied and pasted it in my woocommerce-template.php in the part where I need it to use, but it displays all the categories and none subcategories; please take a look at the link I posted above, it should be like that but with only one category and its subcategories
    – Yhis
    Commented May 30, 2013 at 11:20
  • I took a quick look at the code from the questions you linked. What you are lloking to do is only do whats associated to $sub_cats = get_categories( $args2 ) and the $args2 = array( ... ) part, but that said, what i suggested is pretty much the same. So I'm not really sure why it isn't working for you. Commented May 30, 2013 at 11:36
  • if I remove function woocommerce_subcats_from_parentcat($parent_cat) { it works, but shows only main categories, not subcategories, if I copy and paste your code as it is, it shows blank, none.
    – Yhis
    Commented May 30, 2013 at 11:49
  • Ok, it shows subcategories and not main category, but only if I put '23', with '21' it's all blank.. thanks for your help, if you could fix this last little bug it would be great for me
    – Yhis
    Commented May 30, 2013 at 12:27
3

Here is the code that worked for me in a page template (my parent id was 7):

<?php $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' => 7, )); 
        foreach($wcatTerms as $wcatTerm) : 
        $wthumbnail_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
        $wimage = wp_get_attachment_url( $wthumbnail_id );
    ?>
    <div><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>">
    <?php if($wimage!=""):?><img src="<?php echo $wimage?>" class="aligncenter"><?php endif;?></a>
    <h3 class="text-center"><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a></h3>
    </div>
    <?php endforeach; ?> 
1
  • A simpler form of @mikes's answer without the image option - can replace lines 6-10 with: <a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
    – Jon
    Commented May 27, 2015 at 12:14

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