get_the_terms child terms for current post/custom post only

Inside a Post / Custom Post type loop, I am trying to get child terms selected on the edit screen of that post/custom post.

For Example:

Zoologist ( Parent )

Pre Zoologist
Post Zoologist

Now if for any specific post / custom post if users selected all of above taxonomies including Zoologist (Parent), Pre Zoologist, Post Zoologist.

Using get_the_terms function i only want to get child taxomony, in this case Pre Zoologist, Post Zoologist.

I tried this code but it is not working:

$terms = get_the_terms( get_the_ID(), ‘product_cat’ );

foreach ( $terms as $term ){
if ( $term->parent == 0 ) {
echo ‘<div class=”parent ‘.$term->slug.'”>’ . $term->name . ‘</div>’;
}

}

Please let me know where am I making a mistake.

Read more here:: get_the_terms child terms for current post/custom post only

Leave a Reply

Your email address will not be published. Required fields are marked *