首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >get_terms返回错误

get_terms返回错误
EN

Stack Overflow用户
提问于 2011-03-30 19:20:36
回答 2查看 3.5K关注 0票数 0

您好,当我尝试通过此代码在主题选项中get_terms();

代码语言:javascript
复制
$catalogs_terms = get_terms( 'catalogs' );
   $mycatalogs = array( -1 => 'Select a catalog' );
   if ( $catalogs_terms ) {
      foreach ( $catalogs_terms as $catalog_term ) {
         $mycatalogs[$catalog_term->term_id] = $catalog_term->name;
      }
   }

返回值为空,但这段代码在页面中的任何位置都工作得很好。当我尝试print_r( $catalogs_terms )输出时,我得到了错误

代码语言:javascript
复制
Array ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid Taxonomy ) ) [error_data] => Array ( ) )

我不明白我哪里错了?我的注册分类法函数

代码语言:javascript
复制
    add_action( 'init', 'my_taxonomies', 0 );

function my_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name' => _x( 'Catalogs', 'taxonomy general name' ),
        'singular_name' => _x( 'Catalog', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Catalogs', 'mytextdomain' ),
        'all_items' => __( 'All Catalogs', 'mytextdomain' ),
        'parent_item' => __( 'Parent Catalog', 'mytextdomain' ),
        'parent_item_colon' => __( 'Parent Catalog:', 'mytextdomain' ),
        'edit_item' => __( 'Edit Catalog', 'mytextdomain' ), 
        'update_item' => __( 'Update Catalog', 'mytextdomain' ),
        'add_new_item' => __( 'Add New Catalog', 'mytextdomain' ),
        'new_item_name' => __( 'New Catalog Name', 'mytextdomain' ),
        'menu_name' => __( 'Catalogs', 'mytextdomain' ),
    );  

    // register catalogs hierarchical (like categories)
    register_taxonomy( 'catalogs',
        array( 'news' ),
        array( 'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'public' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'catalogs' )
        )
    );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-01 04:46:08

我已经在t31os的帮助下解决了这个问题

你可以找到更多的here

票数 1
EN

Stack Overflow用户

发布于 2014-07-04 22:06:28

这应该会让你走上正轨。

代码语言:javascript
复制
$qt = 'SELECT * FROM '.$wpdb->terms.' AS t INNER JOIN '.$wpdb->term_taxonomy.' AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy =  "<-- your taxonomy -->" AND tt.count > 0 ORDER BY  t.term_id DESC LIMIT 0 , 30';

$terms = $wpdb->get_results($qt, ARRAY_A);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5485603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档