Woocommerce 将属性标签设置为图像

Woocommerce 将属性标签设置为图像,woocommerce,product,Woocommerce,Product,我使用的是woocommerce,在商店产品页面上显示的是`属性标签:属性值 例如:Tire:Snow 有没有办法将属性标签显示为图像 add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25); function isa_woocommerce_all_pa(){ global $product; $attributes = $product->get_attributes

我使用的是
woocommerce
,在商店产品页面上显示的是`属性标签:属性值

例如:
Tire:Snow

有没有办法将属性标签显示为图像

add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
function isa_woocommerce_all_pa(){
    global $product;

    $attributes = $product->get_attributes();

    if ( ! $attributes ) return;

    $out = '<ul class="custom-attributes">';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->get_variation() ) continue; // skip variations

        if ( $attribute->is_taxonomy() ) {
            $taxonomy = $attribute->get_name();
            $taxo_obj = $attribute->get_taxonomy_object();
            $name = $taxo_obj->name;
            $label = $taxo_obj->label;

            $out .= '<li class="' . esc_attr( $taxonomy ) . '">';

            ## ATTRIBUTE IMAGE ##
            // For a child theme use get_stylesheet_directory_uri() instead.
            $out .= '<img class="attribute-image" src="'.get_template_directory_uri().'/images/attributes/'.$name.'.jpg" alt="Attribute '.$label.'"/> ';
            $out .= '<span class="attribute-values">';

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

            foreach ( $terms as $term_name )
                $term_names['name'] = $term_name;

            $out .= implode(', ', $term_names);
            $out .= '</span></li>';

        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($taxonomy) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<span class="attribute-label">' . $taxonomy . ': </span> ';
            $out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
        }
    }
    $out .= '</ul>';

    echo $out;
}
add_action('woocommerce_single_product_summary','isa_woocommerce_all_pa',25);
函数isa_woo commerce_all_pa(){
全球$产品;
$attributes=$product->get_attributes();
如果(!$attributes)返回;
$out='
    '; foreach($attributes作为$attribute){ 如果($attribute->get_variation())继续;//跳过变体 如果($attribute->is_taxonomy()){ $taxonomy=$attribute->get_name(); $taxo_obj=$attribute->get_taxonomy_object(); $name=$taxo_obj->name; $label=$taxo_obj->label; $out.='
  • ”; ##属性图像## //对于子主题,请改用get_stylesheet_directory_uri()。 $out.=''; $out.=''; $terms=wp\u get\u post\u terms($product->get\u id(),$taxonomy,array('fields'=>'names'); foreach($terms作为$term\u名称) $term\u name['name']=$term\u name; $out.=内爆(“,”,$term_name); $out.='
  • '; }否则{ $value_string=内爆(“,”,$attribute->get_options()); $out.='
  • ”; $out.=''.$taxonomy'.:'; $out.=''.esc_html($value_string)。'
  • ; } } $out.='
'; 回音$out; }
自从WooCommerce 3.2+以来,WC\u产品\u属性对象语法发生了变化。我已经更正了代码,它也应该适用于您…这将显示在单个产品页面上(但不会显示在商店页面上):

add_action('woocommerce_single_product_summary','isa_woocommerce_all_pa',25);
函数isa_woo commerce_all_pa(){
全球$产品;
$attributes=$product->get_attributes();
如果(!$attributes)返回;
$out='
    '; foreach($attributes作为$attribute){ 如果($attribute->get_variation())继续;//跳过变体 如果($attribute->is_taxonomy()){ $taxonomy=$attribute->get_name(); $taxo_obj=$attribute->get_taxonomy_object(); $name=$taxo_obj->attribute_name;//attribute_label;//get_id(),$taxonomy,array('fields'=>'names'); foreach($terms作为$term\u名称) $term\u name['name']=$term\u name; $out.=内爆(“,”,$term_name); $out.=''; }否则{ $value_string=内爆(“,”,$attribute->get_options()); $out.='
  • ”; $out.=''.$taxonomy'.:'; $out.=''.esc_html($value_string)。'
  • ; } } $out.='
'; 回音$out; }
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

由于产品属性没有图像,您应该在活动主题a文件夹图像(如果不存在)和子文件夹属性内创建一个


对于每个产品属性,必须在此子文件夹属性中添加一个图像,该名称将是属性(slug)的名称。例如,对于“
Color
”属性,您必须添加一个名为
Color.jpg

的图像,因为WooCommerce 3.2+的WC\u产品属性对象语法发生了变化。我已经更正了代码,它也应该适用于您…这将显示在单个产品页面上(但不会显示在商店页面上):

add_action('woocommerce_single_product_summary','isa_woocommerce_all_pa',25);
函数isa_woo commerce_all_pa(){
全球$产品;
$attributes=$product->get_attributes();
如果(!$attributes)返回;
$out='
    '; foreach($attributes作为$attribute){ 如果($attribute->get_variation())继续;//跳过变体 如果($attribute->is_taxonomy()){ $taxonomy=$attribute->get_name(); $taxo_obj=$attribute->get_taxonomy_object(); $name=$taxo_obj->attribute_name;//attribute_label;//get_id(),$taxonomy,array('fields'=>'names'); foreach($terms作为$term\u名称) $term\u name['name']=$term\u name; $out.=内爆(“,”,$term_name); $out.=''; }否则{ $value_string=内爆(“,”,$attribute->get_options()); $out.='
  • ”; $out.=''.$taxonomy'.:'; $out.=''.esc_html($value_string)。'
  • ; } } $out.='
'; 回音$out; }
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

由于产品属性没有图像,您应该在活动主题a文件夹图像(如果不存在)和子文件夹属性内创建一个


对于每个产品属性,必须在此子文件夹属性中添加一个图像,该名称将是属性(slug)的名称。例如,对于“
Color
”属性,您必须添加一个名为
Color.jpg

的图像。请在您的问题中添加用于在商店页面上显示
属性标签:属性值
的代码……好的,所以我从您1个月前的一篇帖子中获取了代码,按照您所描述的做了一切,但是图像没有出现。好的,在代码中找到了罪犯…更新了。请在问题中添加用于显示属性标签:属性值的代码
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
function isa_woocommerce_all_pa(){
    global $product;

    $attributes = $product->get_attributes();

    if ( ! $attributes ) return;

    $out = '<ul class="custom-attributes">';

    foreach ( $attributes as $attribute ) {

        if ( $attribute->get_variation() ) continue; // skip variations

        if ( $attribute->is_taxonomy() ) {
            $taxonomy = $attribute->get_name();
            $taxo_obj = $attribute->get_taxonomy_object();
            $name = $taxo_obj->attribute_name; // <== Corrected
            $label = $taxo_obj->attribute_label; // <== Corrected

            $out .= '<li class="' . esc_attr( $taxonomy ) . '">';

            ## ATTRIBUTE IMAGE ##
            // For a child theme use get_stylesheet_directory_uri() instead.
            $out .= '<img class="attribute-image" src="'.get_template_directory_uri().'/images/attributes/'.$name.'.jpg" alt="Attribute '.$label.'"/> ';
            $out .= '<span class="attribute-values">';

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

            foreach ( $terms as $term_name )
                $term_names['name'] = $term_name;

            $out .= implode(', ', $term_names);
            $out .= '</span></li>';

        } else {
            $value_string = implode( ', ', $attribute->get_options() );
            $out .= '<li class="' . sanitize_title($taxonomy) . ' ' . sanitize_title( $value_string ) . '">';
            $out .= '<span class="attribute-label">' . $taxonomy . ': </span> ';
            $out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
        }
    }
    $out .= '</ul>';

    echo $out;
}