Use get_term_children to get the sub category of a parent category for the current post

Don’t need get_term_children() at all. Just loop through get_the_terms() to get what’s needed.

global $post;
$brands_id = get_term_by('slug', 'brands', 'product_cat');

$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
    if($term->parent === $brands_id->term_id) {
        echo $term->name;
        break;
    }
}

Leave a Comment

tech