Skip to content

Instantly share code, notes, and snippets.

@marrisonlab
Created September 27, 2025 06:29
Show Gist options
  • Select an option

  • Save marrisonlab/b02883c4a594ca733aedd3ce88406443 to your computer and use it in GitHub Desktop.

Select an option

Save marrisonlab/b02883c4a594ca733aedd3ce88406443 to your computer and use it in GitHub Desktop.
Display brand in single product page (woo)
function gl_brand_logo_shortcode( $atts ) {
if ( is_admin() ) return '';
$atts = shortcode_atts([
'size' => 'medium', // thumbnail|medium|large|full o 300x150
'class' => 'brand-logo',
'link' => 'yes', // yes|no
'tax' => '' // lascia vuoto per auto-rilevare
], $atts, 'brand_logo');
if ( ! is_product() ) return '';
$product_id = get_the_ID();
$taxonomies = array_filter([
$atts['tax'],
'pwb-brand', // Perfect Brands
'product_brand', // WooCommerce Brands / YITH
'yith_product_brand' // alcune installazioni YITH
]);
$term = null;
$brand_tax = null;
foreach ( $taxonomies as $tax ) {
if ( ! $tax ) continue;
$terms = get_the_terms( $product_id, $tax );
if ( ! empty($terms) && ! is_wp_error($terms) ) {
$term = array_shift($terms);
$brand_tax = $tax;
break;
}
}
if ( ! $term ) return '';
// Prova meta key comuni (Perfect Brands, WC Brands/YITH, e un campo JetEngine personalizzato)
$meta_keys = ['pwb_brand_image', 'thumbnail_id', 'brand_logo'];
$image_id = 0;
foreach ( $meta_keys as $key ) {
$val = get_term_meta( $term->term_id, $key, true );
if ( $val ) { $image_id = is_numeric($val) ? (int)$val : 0; break; }
}
if ( ! $image_id ) return '';
$img = wp_get_attachment_image( $image_id, $atts['size'], false, [
'class' => esc_attr($atts['class']),
'alt' => esc_attr($term->name),
'loading' => 'lazy'
]);
if ( ! $img ) return '';
if ( $atts['link'] === 'yes' ) {
$url = get_term_link( $term, $brand_tax );
if ( ! is_wp_error($url) ) {
$img = sprintf('<a href="%s" class="brand-logo-link" aria-label="%s">%s</a>',
esc_url($url),
esc_attr($term->name),
$img
);
}
}
return $img;
}
add_shortcode( 'brand_logo', 'gl_brand_logo_shortcode' );
@marrisonlab
Copy link
Author

[brand_logo size="custom-130x100" link="yes"]

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