![](/img/trans.png)
[英]WordPress: Why Can't get_terms() See Custom Taxonomy Registered inside Class?
[英]WordPress get_terms() called by wp_axax_ inside a class method, returns “Invalid taxonomy”
wp_axax_在类方法中调用的WordPress get_terms(),返回“无效分类法”代码示例:
class MyClass{
public function __construct(){
add_action('wp_ajax_get-terms-action',array($this,'get_terms_method'));
add_action('wp_ajax_nopriv_get-terms-action',array($this,'get_terms_method'));
}
public function get_terms_method(){
$result = get_terms('taxonomy_name');
}
}
之所以会出现“无效分类法”消息,是因为进行ajax调用时,由于尚未初始化所有插件,因此并未注册所有分类法。
解决方案是在初始化后启动ajax,这样就可以了
例如
class MyClass {
public function __construct() {
add_action( 'init', function() {
//add the ajax hook only when all the taxonomies have been registered
add_action('wp_ajax_get-terms-action', function() {
$result = get_terms( 'taxonomy_name' );
}
}, 99);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.