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

如何在WooCommerce中获取并仅显示当前单个商品页面的商品标签

Hey there! Let's break down how to handle your two WooCommerce needs: fetching only the current product's tags, and displaying those tags exclusively on the single product page.

1. Get the Current Product's Tags

On a single product page, WooCommerce makes the $product object globally available, so you can use WordPress's built-in get_the_terms() function to pull the tags specifically for that product. Here's a quick code snippet:

// Make sure we're on a single product page
if ( is_product() ) {
    global $product;
    // Fetch the product tags for the current product
    $current_product_tags = get_the_terms( $product->get_id(), 'product_tag' );
    
    // Check if we have valid tags (not an error)
    if ( $current_product_tags && ! is_wp_error( $current_product_tags ) ) {
        // Loop through tags to use them as needed
        foreach ( $current_product_tags as $tag ) {
            // Example: Print each tag name
            echo esc_html( $tag->name ) . ', ';
        }
    }
}

This grabs only the tags assigned to the product you're currently viewing—no extra tags from other products.

2. Display Only Current Product's Tags on Single Product Page

By default, WooCommerce's single product template should already show only the current product's tags in the meta section. But if your theme or custom code is pulling all tags instead, here's how to fix it:

Option 1: Replace the Default Meta Section with Custom Tag Display

First, remove the default WooCommerce meta output (which includes tags, categories, etc.), then add your own function to show just the current product's tags:

// Remove the default meta section from the product summary
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );

// Add our custom tag display in its place
add_action( 'woocommerce_single_product_summary', 'custom_display_current_product_tags', 40 );

function custom_display_current_product_tags() {
    global $product;
    
    // Exit if no product object exists
    if ( ! $product ) return;
    
    $product_tags = get_the_terms( $product->get_id(), 'product_tag' );
    
    if ( $product_tags && ! is_wp_error( $product_tags ) ) {
        echo '<div class="product-tags">';
        echo '<strong>' . __( 'Tags:', 'woocommerce' ) . '</strong> ';
        
        // Build links for each tag (like the default output)
        $tag_links = array();
        foreach ( $product_tags as $tag ) {
            $tag_link = get_term_link( $tag, 'product_tag' );
            $tag_links[] = '<a href="' . esc_url( $tag_link ) . '" rel="tag">' . esc_html( $tag->name ) . '</a>';
        }
        
        // Print tags separated by commas
        echo implode( ', ', $tag_links );
        echo '</div>';
    }
}

Option 2: Edit Your Theme's Template File

If your theme has a custom meta template (usually located at your-theme/woocommerce/single-product/meta.php), open that file and replace any code that queries all product tags with the snippet from section 1. Make sure it's using the current product's ID instead of a general query for all tags.

Add this code to your theme's functions.php file, or a custom plugin (recommended to avoid losing changes when updating your theme).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:25:04