[英]get_terms gives “invalid taxonomy” from plugin
我正在為 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);
}
我有同樣的問題。 對於 Woocomerce,您可以通過在functions.php 中添加以下代碼來解決:
register_taxonomy( 'product_cat', array('product'), array() );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.