gpt4 book ai didi

php - 获取 WooCommerce 中的所有产品类别

转载 作者:行者123 更新时间:2023-12-01 00:13:34 25 4
gpt4 key购买 nike

我正在尝试在 WooCommerce 中获取产品类别,但 get_terms() 函数对我不起作用。我得到一个空数组。

我做错了什么?如何获取所有 Woocommerce 产品类别术语?

最佳答案

产品类别是 WooCommerce 产品使用的 “自定义分类法”product_cat

您需要使用 get_terms() 正确的分类法 ,这样:

// Get Woocommerce product categories WP_Term objects
$categories = get_terms( ['taxonomy' => 'product_cat'] );

// Getting a visual raw output
echo '<pre>'; print_r( $categories ); echo '</pre>';

您还可以使用 get_terms() 获取空的产品类别,例如:
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

测试和工作(WordPress 3.5+ 和 WooCommerce 2.4+)......两者都适合你。

你会得到类似的东西:
Array
(
[0] => WP_Term Object
(
[term_id] => 83
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 83
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
[meta_value] => 1
)

[2] => WP_Term Object
(
[term_id] => 11
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
[meta_value] => 2
)
// … and so on …
)

关于php - 获取 WooCommerce 中的所有产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com