我正在使用WooCommerce为WordPress编写插件。我需要在插件的管理页面中获得WooCommerce产品类别(分类'product_cat'
)列表(请注意“管理页面”上的强调)。但是,在通过分类法调用和进行过滤之后,会导致错误(我简化了代码。子菜单类位于其他地方,据我所知它不会干扰):get_categories()
get_terms()
add_action( 'init', 'myplugin_admin_settings' );
function myplugin_admin_settings() {
$plugin = new Submenu( new MyPlugin_Admin_Page('myplugin_options') );
$plugin->init();
}
class MyPlugin_Admin_Page {
public function __construct($option_name) {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false
);
var_export(get_categories( $args )); // Prints 'error'
var_export(get_terms( $args )); // Prints 'error'
var_export(get_terms()); // Prints the whole list of terms including those belonging to 'product_cat' taxonomy
}
}
打印的错误是:
WP_Error::__set_state(array( 'errors' => array ( 'invalid_taxonomy' => array ( 0 => 'Invalid taxonomy.', ), ), 'error_data' => array ( ),))array ( '' => NULL,)
这表明分类法不存在。而且的确,如果我打电话taxonomy_exists('product_cat')
说false
。但是,打印的整个术语列表包括属于的术语'product_cat'
,如果分类法不存在,这似乎是不可能的。那使我丧命。举个例子:
43 =>
WP_Term::__set_state(array(
'term_id' => 56,
'name' => 'Accesorios',
'slug' => 'accesorios',
'term_group' => 0,
'term_taxonomy_id' => 56,
'taxonomy' => 'product_cat',
'description' => '',
'parent' => 0,
'count' => 1,
'filter' => 'raw',
)),
这些调用是在init
挂钩中完成的,因此它们应返回产品类别。
任何人都可以了解正在发生的事情吗?还有其他方法可以在管理页面中获得产品类别吗?
谢谢。
编辑:我也尝试了钩子plugins_loaded
和woocommerce_init
。
好吧,我不知道真正发生了什么,但是现在它正在使用init
钩子工作。我以前尝试过,但是没有,不确定为什么。现在,我只做了2个测试:使用functioncurrent_filter()
检查函数本身是否已正确钩住;并连接到init
包含对的调用的函数get_terms($args)
。然后我注意到它正在工作,并且撤消了更改,一切都在工作。幽灵失败。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句