mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-01 11:52:25 +08:00
https://stackoverflow.com/questions/79631145/display-brand-linked-terms-above-product-title-in-woocommerce-shop-pages
24 lines
927 B
Text
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();
|
|
}
|
|
}
|
|
}
|