使用get_terms()时,分类法会返回无效的分类学错误
我已经注册了自定义分类法。请找到下面的代码,
function register_taxonomy_state() {
$labels = array(
'name' => _x( 'States', 'taxonomy general name' ),
'singular_name' => _x( 'State', 'taxonomy singular name' ),
'search_items' => __( 'Search State' ),
'all_items' => __( 'All States' ),
'parent_item' => __( 'Parent States' ),
'parent_item_colon' => __( 'Parent States:' ),
'edit_item' => __( 'Edit State' ),
'update_item' => __( 'Update State' ),
'add_new_item' => __( 'Add New State' ),
'new_item_name' => __( 'New State Name' ),
'menu_name' => __( 'State' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' =>true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'alumns-reps-states' ],
);
register_taxonomy( 'alumns-reps-states', [ 'alumns-reps' ], $args );
}
add_action( 'init', 'register_taxonomy_state' );
我想显示此分类法下添加的所有条款,为此,我使用了按照代码的使用,
$taxonomies = get_terms( array(
'taxonomy' => 'alumns-reps-states',
'hide_empty' => false
) );
var_dump($taxonomies);
它始终返回'无效分类法'
错误。此代码有什么问题?
I have registered a custom taxonomy.Please find the code below
function register_taxonomy_state() {
$labels = array(
'name' => _x( 'States', 'taxonomy general name' ),
'singular_name' => _x( 'State', 'taxonomy singular name' ),
'search_items' => __( 'Search State' ),
'all_items' => __( 'All States' ),
'parent_item' => __( 'Parent States' ),
'parent_item_colon' => __( 'Parent States:' ),
'edit_item' => __( 'Edit State' ),
'update_item' => __( 'Update State' ),
'add_new_item' => __( 'Add New State' ),
'new_item_name' => __( 'New State Name' ),
'menu_name' => __( 'State' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' =>true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'alumns-reps-states' ],
);
register_taxonomy( 'alumns-reps-states', [ 'alumns-reps' ], $args );
}
add_action( 'init', 'register_taxonomy_state' );
I would like to display all terms added under this taxonomy, and for that i used following code
$taxonomies = get_terms( array(
'taxonomy' => 'alumns-reps-states',
'hide_empty' => false
) );
var_dump($taxonomies);
It is always returning 'Invalid Taxonomy'
error. WHat is wrong with this code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。
对我有帮助:
为此更改您的代码:
I had same issue.
It's help for me:
Change your code for this: