Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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: https://github.com/darkdelphin/WooCommerce-Subcategories-widget/blob/master/woocommerce-subcategories-widget.php

edit: completed the code, tested it, see comments

share|improve this answer
    
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 May 30 '13 at 10:51
    
i edited my answer and the given code, it should do the job now –  ialocin May 30 '13 at 11:13
    
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 May 30 '13 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. –  ialocin May 30 '13 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 May 30 '13 at 11:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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