0

I've installed Woocomerce plugin and custom post type 'product'. There are also categories of this type named 'product_cat'. I need to display name of categories on single-product.php

I've tried in this way:

<?php $term = get_term_by( 'name', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>

But it's unsucessful. When I checked the type that returned to $term, it displayed as boolean.

2
  • You want to display ALL product categories or just the product categories that this product is assigned to?
    – Dan.
    Nov 29 '16 at 13:42
  • just the product categories that this product is assigned to
    – wmdohod
    Nov 29 '16 at 13:45
0

This piece of code will return all categories assigned to current product.

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    $cat_name .= $term->name.', ';
    break;
}
echo $cat_name;

add this code inside single-product.php

Hope this help !

0
0
$terms = get_the_terms( get_the_ID(), 'product_cat' );

foreach ($terms as $term) {
    echo $term->name;
}
2
  • It's working too, but it's return all sub-categories too. Thanks a lot
    – wmdohod
    Nov 29 '16 at 14:55
  • Please edit your answer to provide some context. Why do you think this answers the question? Consult How do I write a good answer? for more information.
    – Pat J
    Nov 29 '16 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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