Skip to content

Instantly share code, notes, and snippets.

@anhnn-mageplaza
Created August 4, 2021 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhnn-mageplaza/45af05de2c31df55bfbb1016ce6f50e1 to your computer and use it in GitHub Desktop.
Save anhnn-mageplaza/45af05de2c31df55bfbb1016ce6f50e1 to your computer and use it in GitHub Desktop.
This is the code to insert WooCommerce Product Subcategories
function woocommerce_get_product_category_of_subcategories( $category_slug ){
$terms_html = array();
$taxonomy = 'product_cat';
$parent = get_term_by( 'slug', $category_slug, $taxonomy );
$children_ids = get_term_children( $parent->term_id, $taxonomy );
foreach($children_ids as $children_id){
$term = get_term( $children_id, $taxonomy );
$term_link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $term_link ) ) $term_link = '';
$terms_html[] = '<a href="' . esc_url( $term_link ) . '" rel="tag" class="' . $term->slug . '">' . $term->name . '</a>';
}
return '<span class="subcategories-' . $category_slug . '">' . implode( ', ', $terms_html ) . '</span>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment