我編寫了一個 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; ?>
- 1 回答
- 0 關注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消