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

在WooCommerce单品页面标题前添加带链接的pa_artist属性

在WooCommerce单品页为产品标题前的Artist属性添加归档链接

嘿,刚好我之前帮朋友处理过一模一样的需求,直接给你上可落地的代码和细节说明,替换你现有的代码就行!

核心思路

你之前已经实现了把pa_artist属性加到标题前,现在只需要给属性术语加上对应属性归档页面的链接——也就是点击后能看到所有该艺术家产品的列表页。关键是要通过术语名称获取到术语对象,再拿到它的归档URL。

单艺术家属性的代码实现

如果你的产品每个只对应一个艺术家,用这段代码替换你之前的标题修改逻辑:

add_filter( 'the_title', 'prepend_linked_artist_to_product_title', 10, 2 );
function prepend_linked_artist_to_product_title( $title, $post_id ) {
    // 仅在单品页面生效,避免影响其他页面标题
    if ( is_product() && get_post_type( $post_id ) === 'product' ) {
        $product = wc_get_product( $post_id );
        // 获取pa_artist属性的术语值
        $artist_terms = $product->get_attribute( 'pa_artist' );
        
        if ( ! empty( $artist_terms ) ) {
            // 提取单个术语名称(处理可能的空格)
            $artist_name = trim( $artist_terms );
            // 通过术语名称获取术语对象
            $artist_term = get_term_by( 'name', $artist_name, 'pa_artist' );
            
            if ( $artist_term && ! is_wp_error( $artist_term ) ) {
                // 获取该艺术家的产品归档链接
                $artist_archive_link = get_term_link( $artist_term );
                // 拼接带链接的前缀,注意做安全转义
                $linked_artist_prefix = '<a href="' . esc_url( $artist_archive_link ) . '">' . esc_html( $artist_name ) . '</a> – ';
                // 组合成新标题
                $title = $linked_artist_prefix . $title;
            }
        }
    }
    return $title;
}

多艺术家属性的兼容处理

如果你的产品可能关联多个艺术家(属性值用逗号分隔),可以用这段代码循环生成每个带链接的术语:

add_filter( 'the_title', 'prepend_linked_artists_to_product_title', 10, 2 );
function prepend_linked_artists_to_product_title( $title, $post_id ) {
    if ( is_product() && get_post_type( $post_id ) === 'product' ) {
        $product = wc_get_product( $post_id );
        $artist_terms = $product->get_attribute( 'pa_artist' );
        
        if ( ! empty( $artist_terms ) ) {
            // 拆分多个艺术家名称
            $artist_names = array_map( 'trim', explode( ',', $artist_terms ) );
            $linked_artists = [];
            
            foreach ( $artist_names as $name ) {
                $artist_term = get_term_by( 'name', $name, 'pa_artist' );
                if ( $artist_term && ! is_wp_error( $artist_term ) ) {
                    $link = '<a href="' . esc_url( get_term_link( $artist_term ) ) . '">' . esc_html( $name ) . '</a>';
                    $linked_artists[] = $link;
                }
            }
            
            if ( ! empty( $linked_artists ) ) {
                // 用逗号分隔多个艺术家链接
                $artist_prefix = implode( ', ', $linked_artists ) . ' – ';
                $title = $artist_prefix . $title;
            }
        }
    }
    return $title;
}

重要注意事项

  • 确保属性可归档:在WooCommerce后台的「产品 > 属性 > pa_artist」设置里,一定要勾选「启用归档」选项,否则get_term_link会返回错误。
  • 安全转义不能少:用esc_url处理链接、esc_html处理文本,避免出现XSS安全漏洞。
  • 代码放置位置:把代码加到当前主题的functions.php文件末尾,或者用自定义功能插件存放,不要直接修改WooCommerce核心文件。

内容的提问来源于stack exchange,提问作者Solarside

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 14:52:52