programing

WooCommerce에서만 현재 제품의 제품 태그를 가져옵니다.

goodjava 2023. 3. 31. 22:22

WooCommerce에서만 현재 제품의 제품 태그를 가져옵니다.

모든 제품 태그가 아닌 현재 단일 제품 페이지의 제품 태그만 표시하려면 어떻게 해야 합니까?

나는 대부분의 인기 태그에 대한 질문을 찾았지만 그것에 대한 질문은 하지 않았다.

WooCommerce 'product_tag' 커스텀 분류법 및 정의된 제품 ID에 대해 wp_get_post_terms() 함수를 다음과 같이 사용할 수 있습니다.

$output = array();

// get an array of the WP_Term objects for a defined product ID
$terms = wp_get_post_terms( get_the_id(), 'product_tag' );

// Loop through each product tag for the current product
if( count($terms) > 0 ){
    foreach($terms as $term){
        $term_id = $term->term_id; // Product tag Id
        $term_name = $term->name; // Product tag Name
        $term_slug = $term->slug; // Product tag slug
        $term_link = get_term_link( $term, 'product_tag' ); // Product tag link

        // Set the product tag names in an array
        $output[] = '<a href="'.$term_link.'">'.$term_name.'</a>';
    }
    // Set the array in a coma separated string of product tags for example
    $output = implode( ', ', $output );

    // Display the coma separated string of the product tags
    echo $output;
}

테스트 및 동작.

교환할 수 있습니다.get_the_id()동적 제품 ID 변수도 사용합니다.

이제 이 기능을 사용하여 제품의 태그 목록을 가져올 수 있습니다.전후 요소와 함께 구분자를 제공할 수 있습니다.

<?php
    global $product;
?>
    <div class="product-tags">
        <?php echo wc_get_product_tag_list( $product->get_id(), ', ' ); ?>
    </div>

언급URL : https://stackoverflow.com/questions/48668811/get-the-product-tags-for-the-current-product-only-in-woocommerce