0

I am using WooCommerce in WordPress. I have few categories of products like these,

Example:

Product 1 Category (//parent)
-- Category 1
-- Category 2
-- Category 3
-- Category 4
Product 2 Category (//parent)
-- Category 10
-- Category 11
-- Category 12
-- Category 13

If I am in a archive page for Category 2(sub) How can I get all other category name under the parent category (Product 1 Category) as a list.

what i'm trying

$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>';
}

Out put should :

.Category 1
.Category 2
.Category 3
.Category 4

I have use below code which works fine in single-product.php page -

<?php 
 $parent = get_category_parents( $cat, true, ' &raquo; ' ); 
echo $product->get_categories( ', ', '<span>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); 
?>
2
  • 1
    You must use get_terms. Woocommerce uses custom taxonomies, and not the build in taxonomy category Commented Jul 30, 2015 at 4:21
  • I have Updated the question please check.
    – Exclutips
    Commented Jul 30, 2015 at 4:48

2 Answers 2

0

I usually do this by getting data from database

$result = mysql_query("SELECT * FROM wp_terms JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_id = wp_terms.term_id 
WHERE wp_term_taxonomy.parent = ".$parent_cat_ID." 
AND wp_term_taxonomy.taxonomy = 'product_cat'");
1
  • Please properly format your code. Everything is in one lllllloooooonnnnnngggggggg line which makes is difficult to read and to keep track of Commented Jul 30, 2015 at 10:09
0

I did this to solve the problem

  <?php 
    $terms = get_the_terms( $post->cat_ID , 'product_cat' );
    foreach ($terms as $term) { 
        $term_id = $term->term_id;
        $term_link = get_term_link( $term, $taxonomy );
        $term_name = $term->name;
        echo '<a class="cat-box" href="' . $term_link . '"><span class="cat-name">' . $term_name . '</span></a>';   
    }
    ?>

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.