Take the 2-minute tour ×
WordPress Development Stack Exchange 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

2 Answers 2

You can very easily do so using woocommerce shortcodes

echo do_shortcode('[product_categories number="12" columns="4"]');

if you want the shorcode to show all categories do so:

echo do_shortcode('[product_categories number="12" columns="4" parent="0"]');

I believe I have made it clear enough for you to understand.

You can also take a look at how it's done in content_product-cat.php and use your own custom loop, the shortcodes above will output the content of this file btw.

share|improve this 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.