我编写了一个 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 关注
- 111 浏览
添加回答
举报
0/150
提交
取消