Wordpress get current post term parent by child ID
That's what the get_term
function is for:
$term_id = 21; // Lucky number :)$child_term = get_term( $term_id, 'category' );$parent_term = get_term( $child_term->parent, 'category' );
Replace 'category'
with whatever taxonomy you're using.
$terms = get_the_terms( $_product->id , 'product_cat');if($terms) { foreach( $terms as $term ) { $term = get_term_by("id", $term->parent, "product_cat"); if ($term->parent > 0) { $term = get_term_by("id", $term->parent, "product_cat"); } $cat_obj = get_term($term->term_id, 'product_cat'); $cat_name = $cat_obj->name; }}echo '<br />('. $cat_name . ')';