[英]Product variation WP_Query with a product category in Woocommerce
[英]wp_query for woocommerce featured product category list is display categories more than once?
我編寫了一個 wp_query 來檢查 woocommerce 中的特色產品。
首先它收集所有類別,然后檢查它們是否有特色產品。 然后顯示類別名稱。 我唯一的問題是它重復了其中每個特色產品的貓名。
我錯過了什么導致循環以這種方式運行的任何想法? 我認為我的代碼也可能有點臃腫。
謝謝
<?php
$fpArgs = array(
'post_type' => 'product',
'tax_query' => array(
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
)
);
$featProds = new WP_Query( $fpArgs );
if ($featProds->have_posts()):
?>
<section class="standard featuredProducts">
<?php $cat_terms = get_terms('product_cat'); ?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
foreach ( $cat_terms as $cat_term ):
$cat_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( $cat_term->slug ),
'operator' => 'IN'
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
)
)
) );
?>
<?php
if ( $cat_query->have_posts() ) :
while ( $cat_query->have_posts() ) :
$cat_query->the_post();
$catName = $cat_term->name;
?>
<li class="nav-item" role="presentation">
<a class="nav-link" id="cat-tab">
<?php echo $catName; ?>
</a>
</li>
<?php
endwhile;
endif;
endforeach;
?>
</ul>
</section>
<?php endif; ?>
我已經想出了一個解決方案...
我將所有類別添加到一個數組中,並將其用於 select 僅具有 if 語句的唯一類別。
遵循這個解決方案,它就像一個魅力https://wordpress.stackexchange.com/questions/154970/remove-duplicated-values-from-a-loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.