我正在为 woocommerce 构建一个插件,但遇到了一些麻烦。我正在尝试获取所有可用的产品类别。
代码看起来像这样:
$cats = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0));
print_r($cats);
这给了我
WP_Error Object
(
[errors:WP_Error:private] => Array
(
[invalid_taxonomy] => Array
(
[0] => Invalid taxonomy
)
)
[error_data:WP_Error:private] => Array
(
)
)
我需要把它挂接到一些特殊的初始化程序上吗?我在 functions.php 中尝试了相同的代码,但出现了相同的错误。
编辑: 是的,我找到了解决问题的方法。我加了
add_action('init', 'runMyPlugin');
成功了!
最佳答案
只需添加完整的代码示例
add_action('init', 'my_get_woo_cats');
function my_get_woo_cats() {
$cats = get_terms( array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0) );
print_r($cats);
}
关于php - get_terms 从插件中给出 "invalid taxonomy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26564012/