使用get_terms()时,分类法会返回无效的分类学错误

发布于 2025-02-11 17:01:59 字数 1482 浏览 2 评论 0原文

我已经注册了自定义分类法。请找到下面的代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-02-18 17:01:59

我也有同样的问题。
对我有帮助:
为此更改您的代码:

$args = array(
    'taxonomy' => 'alumns-reps-states', 
    'hide_empty' => false
);
$the_query = new WP_Term_Query($args);
var_dump($the_query->get_terms());

I had same issue.
It's help for me:
Change your code for this:

$args = array(
    'taxonomy' => 'alumns-reps-states', 
    'hide_empty' => false
);
$the_query = new WP_Term_Query($args);
var_dump($the_query->get_terms());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文