【问题标题】:get_terms WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy. ) ) [error_data] => Array ( ) ) [duplicate]get_terms WP_Error Object ([errors] => Array ([invalid_taxonomy] => Array ([0] => Invalid taxonomy.)) [error_data] => Array ()) [重复]
【发布时间】:2018-07-28 06:24:51
【问题描述】:

如何解决 get_terms 函数的 WP_Error 对象? 我在coustom-post-type.php 文件中注册了自定义分类法tax_alone_event。 并包含在functions.php 文件中。 这是我的代码

function get__terms_list() {
  $terms = get_terms('tax_alone_event');
  return $terms ;

} //End get_terms_list()..

print_r(get__terms_list());

我在coustom-post-type.php 文件中注册了自定义帖子和自定义分类代码

function Alone_setup_post_type_project() {
    // event custom post and tax 
    $alone_event_args = array(
        'labels'             => array(
            'name'               => _x( 'Event', 'post type general name', 'alonefoundation' ),
             'add_new'            => _x( 'Add New', 'Event', 'alonefoundation' ),
             'add_new_item'       => __( 'Add New Event', 'alonefoundation' ),
             'new_item'           => __( 'New Event', 'alonefoundation' ),
             'edit_item'          => __( 'Edit Event', 'alonefoundation' ),
             'view_item'          => __( 'View Event', 'alonefoundation' ),
             'all_items'          => __( 'All Event', 'alonefoundation' ),
        ),
        'description'        => __( 'Description.', 'alonefoundation' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'event' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => true,
        'taxonomies'            => array('tax_alone_event'),
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail')
    );
    register_post_type( 'alone-event', $alone_event_args );
    $alone_event_tax = array(
        'label' => __( 'Category' ),
        'rewrite' => array( 'slug' => 'event_tax' ),
        'hierarchical' => true,
    ) ;
    register_taxonomy( 'tax_alone_event', array('alone-event'), $alone_event_tax );
}

add_action( 'init', 'Alone_setup_post_type_project');

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    get_terms() 函数将只显示默认的 WordPress taxonomies,除非您将其挂接到 init。

    function get__terms_list() {
      $terms = get_terms('tax_alone_event',array(
        'hide_empty' => false
     ));
      print_r($terms);
    } //End get_terms_list()..
    
    //print_r(get__terms_list());
    
    add_action('init','get__terms_list');
    

    输出:

    Array ( 
    [0] => WP_Term Object ( 
    [term_id] => 28 
    [name] => Custom Taxonomy 
    [slug] => custom-taxonomy 
    [term_group] => 0 
    [term_taxonomy_id] => 28 
    [taxonomy] => tax_alone_event 
    [description] => 
    [parent] => 0 
    [count] => 0 
    [filter] => raw )
     )
    

    【讨论】:

    • 还是同样的错误
    • 我刚刚在我的本地机器上运行了这段代码。它工作正常。
    • @MamunurRashid,据我了解,您正在尝试获取所有类别列表吗?
    • @MamunurRashid,您是否在 functions.php 文件中包含 coustom-post-type.php 文件?
    • 是的,我在functions.php 文件中包含coustom-post-type.php 文件。我正在尝试获取特定自定义帖子alone_event 的所有类别/分类法
    【解决方案2】:

    您可以通过两种方式消除错误

    1. 不要使用init钩子,直接调用Alone_setup_post_type_project()
    2. get_terms 函数也与 init hook 一起使用

    因为你正在做的是在wordpress之前使用get_terms函数已经向系统注册了你的帖子类型和分类,因此无法找到它。

    【讨论】:

      【解决方案3】:

      我将您的代码复制给自己,效果很好。
      您在哪个文件中有这部分?

      function get__terms_list() {
        $terms = get_terms('tax_alone_event');
        return $terms ;
      
      } //End get_terms_list()..
      
      print_r(get__terms_list());
      

      print_r(get__terms_list()); 应该在 page.php、footer.php 等文件中。

      print_r(get_taxonomies()); 放在print_r(get__terms_list()); 之前以查看您的分类是否已注册。

      【讨论】:

        【解决方案4】:

        您应该通过 $args 数组中的 'taxonomy' 参数从 4.5.0 - https://developer.wordpress.org/reference/functions/get_terms/ 传递分类。

        function get__terms_list() {
            $terms = get_terms( array( 'taxonomy' => 'tax_alone_event' ) );
            return $terms ;
        }
        
        print_r(get__terms_list());
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 2020-06-20
        • 2022-12-01
        • 2022-12-06
        • 2012-02-29
        • 1970-01-01
        相关资源
        最近更新 更多