WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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;
share|improve this answer

Please use this code.

It's working for me..

$term = get_queried_object()->term_id;
$termid = get_term($term, 'product_cat' );

        $args = array(
            'orderby'       => 'name', 
            'order'         => 'ASC',
            'hide_empty'    => false, 
            'child_of'      => $term, 
            'parent'        =>0
    $subproducts = get_terms( 'product_cat', $args);
    foreach ($subproducts as $subproduct) { 
    echo $subproduct->name; ?>
share|improve this answer
Please don't just dump code, perhaps edit and add some explanation how this might solve the problem. – Mayeenul Islam Feb 28 '15 at 10:59

Your Answer


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.