Show a Product Attribute in Shop Listings
Displays the values of an attribute, such as available colors or sizes, under each product title on shop and category pages.
Opens WPIDE on
Code
add_action( 'woocommerce_after_shop_loop_item_title', function () {
$attribute = 'pa_color'; // Attribute taxonomy or local attribute name.
$label = 'Colors'; // Label shown before the values.
global $product;
if ( ! $product instanceof WC_Product ) {
return;
}
$values = $product->get_attribute( $attribute );
if ( '' === $values ) {
return;
}
echo '<p class="wpide-loop-attribute" style="margin:.3em 0;font-size:.85em;color:#50575e;">' . esc_html( $label . ': ' . $values ) . '</p>';
}, 6 );
Notes
Set $attribute to the attribute slug, for example pa_color for a global "Color" attribute.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.