2

I am wanting to show the current taxonomy product category title in the single product page.

The code below works, but returns all product categories. I need some help singling out the current category.

<?php 
    global $post;
    $args = array( 'taxonomy' => 'product_cat',);
    $terms = wp_get_post_terms($post->ID,'product_cat', $args);

    $count = count($terms); 
    if ($count > 0) {

    foreach ($terms as $term) {
        echo '<div style="direction:rtl;">';
        echo 'Category: ' . $term->name;
        echo '</div>';
    }
}
?>

After a few hours of experimenting and searching, you guys are my last hope for a solution. Thanks.

3

You could use get_the_term_list

echo get_the_term_list( $post->ID, 'product_cat', '<div style="direction:rtl;">', '</div>', '' ); 

The system of displaying 1 category title on a single product page fails if you start adding the item to multiple categories.

I went crazy once on a project where a client was adding portfolio items to two categories and yet somehow wanted me to show the 'right' category title depending. That project led me to create the Radio Buttons for Taxonomies plugin to restrict users to a single term in certain taxonomies.

  • 2
    Just as informational addition using get_the_term_list() is just what $product->get_categories() does. – Nicolai Nov 21 '13 at 1:06
  • @helgatheviking Thanks for taking the time to help. Both solutions work for a single category. – Kapitol Nov 21 '13 at 1:42
  • @ialocin So it does! Learn something new every day. – helgatheviking Nov 21 '13 at 10:52
  • My life motto :) – Nicolai Nov 27 '13 at 22:05
2

In most themes I know this is standard, but yours might differ. Below code is from the meta.php file, which is responsible for showing the meta data - categories, tags - in woocommerce.

Code:

    <?php
            $size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
            echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $size, 'woocommerce' ) . ' ', '.</span>' );
    ?>

Edit:

I gave an extended answer to this problem some time ago:

  • Thanks, ialocin. This code returns all categories. It's not what I am looking for. I am looking for the single category you are in currently to be displayed. – Kapitol Nov 20 '13 at 23:49
  • I guess I misunderstood you, see my edit. @Kapitol – Nicolai Nov 21 '13 at 0:02
  • Your solution does show a single category, but it still is not the current category. You addressed the issue of having multiple top level categories by saying "won't work if a product has multiple main-/toplevel-categories, or at least won't show them all;" – Kapitol Nov 21 '13 at 0:10
  • You're on a single product page, if a product has more then one category of the top level, how can there be a single current category? @Kapitol – Nicolai Nov 21 '13 at 0:13
  • Good question. Could be it's impossible. Maybe it is a breadcrumbs type of solution. Below is what I wanted to achieve. In a category page it lists the current category you are in. Clicking on a product within that category takes you to the product. I want a title stating you came from the previous category. – Kapitol Nov 21 '13 at 0:19

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.