问题
Im trying to echo the products attribute labels inside a class in a woocommerce loop. I have tried:
class="<?php echo wc_attribute_label( $name ); ?>"
but it doesn't work. What am I doing wrong?
Thanks for any help possible!
Kind regards, Richard
回答1:
Thanks to Kharis Sulistiyono i got this answer in the wordpress/woocommerce forums:
<?php
global $product;
$attribute = $product->get_attributes();
$attribute_arr = array();
if( count($attribute) > 0 ){
foreach ($attribute as $key => $value) {
$attribute_arr[] = $key;
}
}
$attributes = implode($attribute_arr, ' ');
?>
/* Print as class value */
class="<?php echo esc_attr($attributes); ?>";
It works like a charm!
回答2:
You can do this:
$term = get_term_by( 'name', $variation_product->get_attribute( 'pa_color' ), 'pa_color' );
This gives you a term object, so if you for instance want the labels slug you can just:
echo $term->slug;
Or ID:
echo $term->id;
来源:https://stackoverflow.com/questions/31516096/how-to-echo-a-attribute-label-in-woocommerce