问题
I am trying to create a template for sub-category where I can display products under sub-category and in parent category page want to display only related sub-categories of the parent category. Can anyone suggest me with the example of code? how to start this.
回答1:
Try the following:
if ( is_product_category() ) {
$terms = get_terms( array('taxonomy' => 'product_cat', 'parent' => get_queried_object_id() ) );
foreach ( $terms as $term ){
$term_link = get_term_link( $term, $taxonomy );
echo '<a class="ccats" href="'.$term_link.'"><span class="label">'.$term->name.'</span></a>';
}
}
来源:https://stackoverflow.com/questions/55968836/woocommerce-display-sub-categories-in-parent-category-page