admin管理员组

文章数量:1122846

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>' ); 
?>

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>' ); 
?>
Share Improve this question edited Jul 30, 2015 at 4:50 Exclutips asked Jul 29, 2015 at 22:38 ExclutipsExclutips 6851 gold badge6 silver badges15 bronze badges 2
  • 1 You must use get_terms. Woocommerce uses custom taxonomies, and not the build in taxonomy category – Pieter Goosen Commented Jul 30, 2015 at 4:21
  • I have Updated the question please check. – Exclutips Commented Jul 30, 2015 at 4:48
Add a comment  | 

2 Answers 2

Reset to default 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'");

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

本文标签: How to get related categorycategories in WordPress archive page