我的自定义分类设备与我的产品一起使用Woocommerce默认产品类别。
我想按照自定义分类法在每个产品类别搜索下列出我的产品。
例如:
设备有价值:devise-1
和devise-2
使用devise-1
进行搜索时,列表将为:
答案 0 :(得分:0)
对不起,如果我理解你的错误。
$your_slug_custom_tax = ( isset($_GET['{your_var_custom_tax}']) ) ? $_GET['{your_var_custom_tax}'] : '';
$your_config = array(
'post_type' => 'product', //If woocommerce native
'posts_per_page' => '-1', //You can change for your preference
'post_status' => 'publish', //status of post or product for show
'tax_query' => array() // this is for you custom search
);
if($your_slug_custom_tax != ''){
$your_config['tax_query'][] = array(
'taxonomy' => '{your slug of taxonomy}',
'field' => 'slug',
'terms' => $your_slug_custom_tax // your taxonomy for show post.
);
}
$your_config = get_posts($your_config);
if ( $your_config ) {
foreach ( $your_config as $mypost ):
print_r($mypost);
endforeach;
}
答案 1 :(得分:0)
我找到了符合我要求的解决方案,但我觉得它很贵。因为它是每个类别的查询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; ?>
`
任何人都有一个简单的解决方案。感谢