Wordpress gets current parent post after child id

On the page I have the identifier of the term child, I need to find out this child parent by the identifier of the child, it is passive, maybe someone can help with this?

+5
source share
2 answers

What function is get_termintended for:

$term_id = 21; // Lucky number :)

$child_term = get_term( $term_id, 'category' );
$parent_term = get_term( $child_term->parent, 'category' );

Replace with 'category'any taxonomy you use.

+6
source
$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 . ')';
+2
source

All Articles