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.

2 Answers
2

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *