您好,当我通过此代码尝试get_terms();
主题选项时
$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 )
输出时我收到错误
Array ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid Taxonomy ) ) [error_data] => Array ( ) )
我不明白我哪里错了?
我的注册分类功能
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' )
)
);
}
答案 0 :(得分:3)
这应该让你走在正确的轨道上......
$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);
答案 1 :(得分:1)