Skip to content

Instantly share code, notes, and snippets.

@shaunkuschel
Created June 29, 2021 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunkuschel/dcdcf1a5d5ea0022ec51a485ac44b314 to your computer and use it in GitHub Desktop.
Save shaunkuschel/dcdcf1a5d5ea0022ec51a485ac44b314 to your computer and use it in GitHub Desktop.
Add Categories to Product Loop Title
//Remove title hook and add in a new one with the product categories added
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'sk_category_loop_product_title', 10 );
function sk_category_loop_product_title() {
echo '<h3>' . get_the_title() . '</h3>';
global $post;
$args = array( 'taxonomy' => 'product_cat',);
$terms = get_the_terms( $post->ID, 'product_cat', $args);
if ( $terms && ! is_wp_error( $terms ) ) :
//Only displayed if the product has at least one category
$cat_links = array();
foreach ( $terms as $term ) {
$cat_links[] = $term->name;
}
$on_cat = join( " ", $cat_links );
?>
<div class="label-group">
<?php echo 'Categories: ' . $on_cat; ?>
</div>
<?php endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment