WooCommerce:返回特定类别中所有产品 ID 的函数 [英] WooCommerce: function that returns all product ID's in a particular category
本文介绍了WooCommerce:返回特定类别中所有产品 ID 的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为问题的标题是不言自明的,但要详细说明...
I think the title of the question is fairly self-explanatory, but to detail it...
我正在尝试在 Woocommerce 中创建自定义类别页面.
I'm trying to make a custom category page in Woocommerce.
我需要的是返回特定类别中的所有产品 ID.
What I need is to return all the product ID's in a particular category.
我看过这篇文章,但它使用了 WP_Query 做事方式,即好丑.
I've seen this post, but it uses the WP_Query way of doing things, which is pretty ugly.
我更喜欢使用 WooCommerce 类中的某些内容来完成此操作.
I'd prefer to use something in the WooCommerce classes to accomplish this.
理想情况下,应该有类似以下的某种功能/方法(但我找不到):
Ideally, there should some sort of function/method like the following (but I can't find it):
get_products_in_category( $category_ID );
// Returns array of product ID's
任何帮助都会很棒.
推荐答案
你在这里:
function get_products_from_category_by_ID( $category_id ) {
$products_IDs = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_id,
'operator' => 'IN',
)
)
) );
return $products_IDs;
}
这篇关于WooCommerce:返回特定类别中所有产品 ID 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文