Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using woo commerce theme for my site. I am trying to display a list of main product category list. I tried this code. But it returns all the main and sub categories.

$product_category = wp_get_post_terms( $post->ID, 'product_cat' );

I need to display just the main product category list. How to do that?

share|improve this question

1 Answer

Check the parent of each category, top-level terms will have a parent value of 0:

$product_category = wp_get_post_terms( $post->ID, 'product_cat' );

foreach( $product_category as $cat ):
    if( 0 == $cat->parent )
        echo $cat->name;
endforeach;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.