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
'programing' 카테고리의 다른 글
| Go를 사용하여 JSON 응답을 제공하려면 어떻게 해야 합니까? (0) | 2023.03.31 |
|---|---|
| 여러 데이터 소스를 구성한 후 JPA 명명 전략을 설정할 수 없음 (Spring 1.4.1 / Hibernate 5.x) (0) | 2023.03.31 |
| Airbnb 스타일 가이드는 왜 함수 이름 추론에 의존하지 말라고 하는가? (0) | 2023.03.26 |
| 스프링 REST 템플릿 포함 제네릭 (0) | 2023.03.26 |
| Oracle의 null 문자열과 빈 문자열 비교 (0) | 2023.03.26 |