woocommerce 分类到菜单_通过自定义分类将Woocommerce产品按类别分组

本文探讨如何将WooCommerce产品按照自定义分类法进行分组,以便在搜索时按设备类别列出。提供了通过修改查询参数实现这一目标的代码示例,但注意到这种方法可能对数据库性能有影响,寻求更优解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的产品具有Woocommerce默认产品类别, 因此具有自定义分类法设备。

我想按自定义分类法在每个产品类别搜索下列出我的产品。

例如:

设备具有价值:devise-1和devise-2

当使用devise-1搜索时, 列表将为:

类别-1

产品1

产品2

类别-2

产品1

产品2

#1

你好!你好吗?我们了解你希望通过按自定义类别类型进行搜索来导出帖子。如果是这样, 我认为这会有所帮助。对不起, 我的英语不好。

对不起, 如果你的问题我理解错了。

$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;

}

#2

我找到了满足我要求的解决方案, 但我认为它很昂贵。因为它是每个类别的查询数据库。

`

$args = array(

'orderby' => 'title', 'order' => 'ASC', 'hide_empty' => TRUE, 'taxonomy' => 'product_cat'

);

$product_categories = get_terms($args);

foreach ($product_categories as $product_category) : ?>

= $product_category->name; // Print Product categories ?>

$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(); ?>

`

任何人都有一个简单的解决方案。谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值