Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Veesibility/437a07e823706596be88 to your computer and use it in GitHub Desktop.
Save Veesibility/437a07e823706596be88 to your computer and use it in GitHub Desktop.
Woocommerce - Display Product Category Title on Shop Page
/** This code should be added to functions.php of your theme **/
/* Woocommerce - Display Product Category Title on Shop Page */
add_action( 'woocommerce_before_shop_loop_item_title', 'vee_add_product_cat', 25);
function vee_add_product_cat()
{
global $product;
$product_cats = wp_get_post_terms($product->id, 'product_cat');
$count = count($product_cats);
foreach($product_cats as $key => $cat)
{
echo
'<span class="vee_category_title">
<span class="vee_category_title_'.$cat->slug.'">'.$cat->name.'</span></span>';
if($key < ($count-1))
{
echo ' ';
}
else
{
echo ' ';
}
}
}
@14ur3nt
Copy link

14ur3nt commented Jan 23, 2022

This works when a category is selected, but also displays the first category when no category is selected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment