显示为 WooCommerce 产品设置的特定产品属性链接术语

问题描述

例如,一个产品的属性 pa_color 有 3 个值(“silver”、“red”和“brown”)。 我想显示三个指向其描述的动态链接

我想向我的产品展示此文字
“此产品有这三种颜色:银色(html 链接)、红色(html 链接)、棕色(html 链接)”

如果它有一个属性,我设法做到了:

if ( is_product() && has_term( 'Silver','pa_color' ) ) {

}

解决方法

使用以下代码显示为产品设置的属性喜欢颜色:

$taxonomy = 'pa_color'; // Here set the product attribute taxonomy
$terms    = wp_get_post_terms( get_the_ID(),$taxonomy ); // Get the terms

if ( ! empty( $terms ) ) {
    $output   = []; // Initializing

    // Loop through the terms set in the product
    foreach( $terms as $term ) {
        $output[] = '<a href="'.get_term_link( $term,$taxonomy ).'">'.$term->name.'</a>';
    }
    // Display
    printf( __("This product has these %s %s: %s."),count($terms),_n( "color","colors",count($terms) ),implode( ',',$output ) );
}

经过测试并有效。