The below code that displays product attributes on product category archive pages and is based on "Display product attributes on specific Woocommerce product category archives page" answer thread.
However, when I add that category on the homepage, It does not display the attributes.
My code version:
// Show Attributes on Product Category Page
add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
global $product;
//$product_attributes = array('pa_size', 'pa_color');
$product_attributes = array('pa_size');
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
if( $value = $product->get_attribute($taxonomy) ){
// The product attribute label name
$label_name = get_taxonomy($taxonomy)->labels->singular_name;
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes-custom">'.implode('<br>', $attr_output).'</div>';
}
I can't figure out how to make it work on my custom homepage that display products and on specific product category pages. For the homepage I know that the conditional tag to be used is is_home()
… But how can I make it work for product category pages too?
Updated - The following code will display specific product attributes on:
is_front_page()
WordPress conditional tagis_product_category()
Woocommerce conditional tagThe code:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Addition: To format each attribute term name in it's own
<span>
tag…Insert in the function code, after this line:
The following:
Related: Display product attributes on specific Woocommerce product category archives page