Code-Snippets-Functions/Execute a function on a child site/WooCommerce/display-brand-linked-terms-above-product-title-in-shop-pages.txt

24 lines
927 B
Text

add_action('woocommerce_before_shop_loop_item_title', 'show_brand_before_title', 10);
function show_brand_before_title() {
global $product;
$taxonomy = 'product_cat'; // Targeted taxonomy (here woocommerce product brands)
$terms = get_the_terms($product->get_id(),$taxonomy); // Get the related product terms
if ( $terms && !is_wp_error($terms) ) {
$brand_list = array(); // Initialize
foreach ($terms as $term) {
$brand_list[] = sprintf('<a href="%s">%s</a>', esc_url( get_term_link($term) ), esc_html($term->name) );
}
if ( $brand_list ) {
echo '</a>'; // Close product link
printf( '<div class="product-brand product-brand-shop-page">%s</div>', implode(', ', $brand_list) ); // Display linked brands
// Add back (reopen) default product link
woocommerce_template_loop_product_link_open();
}
}
}