我有自定义分类设备与我的产品与Woocommerce默认产品类别。
我想把我的产品列在每个产品类别下,根据自定义分类进行搜索。
例如,:
设备有价值:devise-1和devise-2
使用devise-1进行搜索时,列表将是:
发布于 2018-02-13 06:26:34
我找到了一种符合我要求的解决办法,但我认为它很昂贵。因为它是每个类别的查询DB。
`
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => TRUE,
'taxonomy' => 'product_cat'
);
$product_categories = get_terms($args);
foreach ($product_categories as $product_category) : ?>
<h2 class="title ">
<a href=" <?php echo get_term_link($product_category) ?>"> <?= $product_category->name; // Print Product categories ?> </a>
</h2>
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
),
array(
'taxonomy' => 'device',
'field' => 'slug',
'terms' => $device_type,
'operator' => 'IN'
)
),
'post_type' => 'product',
'orderby' => 'sku,'
);
$products = new WP_Query($args);
// Inner Product loop
while ($products->have_posts()): $products->the_post(); ?>
<?php wc_get_template_part('content', 'product'); ?>
<?php endwhile; ?>`
任何人都有一个简单的解决方案。谢谢
https://stackoverflow.com/questions/48753562
复制相似问题