在自定义主页和产品类别档案中显示 WooCommerce 产品属性

Show WooCommerce product attributes in custom home and product category archives

以下代码在产品类别存档页面上显示产品属性,并基于 "" 回答线程。

但是,当我在主页上添加该类别时,它没有显示属性。

我的代码版本:

// 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>';
}

我不知道如何让它在显示产品的自定义主页和特定产品类别页面上运行。对于主页,我知道要使用的条件标签是 is_home()...但是我怎样才能让它也适用于产品类别页面?

已更新 - 以下代码将显示特定产品属性:

代码:

add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
    global $product;

    // The Product attribute to be displayed
    $product_attributes = array('pa_size');

    // The targeted Product category archive pages (slugs)
    $categories = array('t-shirts', 'hoodies', 'socks');

    $output = array(); // Initializing

    // Targetting home page and specific product category archive pages
    if( is_front_page() || is_product_category($categories) ) {

        // Loop through the array of product attributes
        foreach( $product_attributes as $taxonomy ){
            if( taxonomy_exists($taxonomy) ){
                if( $values = $product->get_attribute($taxonomy) ){
                    // The product attribute label name
                    $label_name = get_taxonomy($taxonomy)->labels->singular_name;

                    // Storing attributes for output
                    $output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$values.'</span>';
                }
            }
        }

        // Output attribute name / value pairs, separate by a "<br>"
        echo '<div class="product-attributes-custom">'.implode('<br>', $output).'</div>';
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。


补充: 在它自己的 <span> 标签中格式化每个属性术语名称…

在函数代码中插入这一行之后:

                    // The product attribute label name
                    $label_name = get_taxonomy($taxonomy)->labels->singular_name; 

以下:

                    // convert string of term names to an array
                    $values = explode(', ', $values);
                    // Format each and set back to a string
                    $values = '<span>' . implode('</span> <span>', $values) . '</span>';

相关: