I want to get the current category for my WooCommerce products and display it beneath my products. Currently it gets the category by alphabetical order. This is a little odd when you have a parent category called "clearance" and a child category called "shoes" and it gets the clearance one instead even when going to the shoes category.
The function that is built into the theme right now is posted below.
function gbx_wc_get_cat_forproduct() {
global $smof_data, $product;
if (!$smof_data['show_shop_category'])
return;
$terms = get_the_terms($product->ID, 'product_cat');
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
echo '<a class="product-cat" href="' . get_term_link($term->slug, 'product_cat') . '">' . $term->name . '</a>';
break;
}
}
}
I googled how to do this but my PHP knowledge is limited to say the least. I also seen another question but it didn't seem to help me. --- WooCommerce - get category for product page