Created
June 29, 2021 17:34
-
-
Save shaunkuschel/dcdcf1a5d5ea0022ec51a485ac44b314 to your computer and use it in GitHub Desktop.
Add Categories to Product Loop Title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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