You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改代码实现WooCommerce产品属性术语名称可链接跳转?

问题描述

我希望在WooCommerce单个产品页面的标签区域显示产品属性,并且让属性名称成为指向拥有该属性的所有产品的链接。

我找到了一段可以正常运行的代码,但不知道如何修改来实现上述需求:

function cw_woo_attribute(){
    global $product;
    $attributes = $product->get_attributes();
    if ( ! $attributes ) {
        return;
    }
    $display_result = '';
    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            $cwtax = $terms[0]->taxonomy;
            $cw_object_taxonomy = get_taxonomy($cwtax);
            if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
                $tax_label = $cw_object_taxonomy->labels->singular_name;
            } elseif ( isset( $cw_object_taxonomy->label ) ) {
                $tax_label = $cw_object_taxonomy->label;
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
            $display_result .= $tax_label . ': ';
            $tax_terms = array();
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
            $display_result .= implode(', ', $tax_terms) .  '<br />';
        } else {
            $display_result .= $name . ': ';
            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
    echo $display_result;
}
add_action('woocommerce_single_product_summary', 'cw_woo_attribute', 25);

另外,我之前使用Isabel Castillo开发的「WooCommerce Show Attributes」插件,但该插件出现冲突,导致加入购物车按钮显示错误,所以改用自定义代码方案。


修改方案

只需修改代码中输出属性标签的部分,为其添加对应属性的产品归档链接即可:

修改后的完整代码

function cw_woo_attribute(){
    global $product;
    $attributes = $product->get_attributes();
    if ( ! $attributes ) {
        return;
    }
    $display_result = '';
    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            $cwtax = $terms[0]->taxonomy;
            $cw_object_taxonomy = get_taxonomy($cwtax);
            
            // 获取属性标签文本
            if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
                $tax_label = $cw_object_taxonomy->labels->singular_name;
            } elseif ( isset( $cw_object_taxonomy->label ) ) {
                $tax_label = $cw_object_taxonomy->label;
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
            // 获取该属性的产品归档页面URL
            $taxonomy_url = get_term_link( $cwtax, $cwtax );
            
            // 将属性标签包装为跳转链接
            $display_result .= '<a href="' . esc_url( $taxonomy_url ) . '">' . esc_html( $tax_label ) . '</a>: ';
            
            $tax_terms = array();
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
            $display_result .= implode(', ', $tax_terms) .  '<br />';
        } else {
            // 自定义非分类属性,保持原输出逻辑
            $display_result .= $name . ': ';
            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
    echo $display_result;
}
add_action('woocommerce_single_product_summary', 'cw_woo_attribute', 25);

修改说明

  1. 获取归档链接:使用get_term_link()函数获取对应属性分类的产品归档页面URL,点击该链接会展示所有拥有此属性的产品。
  2. 包装属性标签:将原本直接输出的属性名称,替换为带<a>标签的链接代码,同时用esc_url()和esc_html()确保输出内容的安全性,避免XSS风险。
  3. 非分类属性兼容:对于自定义的非分类属性(未使用WooCommerce分类属性创建的属性),代码保留原输出逻辑,若需要为这类属性添加链接,可自行扩展筛选逻辑。

内容的提问来源于stack exchange,提问作者Michał Nowakowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:05:09