get_terms() 返回无效的分类法 [英] get_terms() returns Invalid Taxonomy
问题描述
我查看了其他示例和解决方案,但一定有我遗漏的地方.我已经尝试了其他帖子的答案建议的所有内容,但无济于事.
I've looked at other examples and solutions, but there has to be something I am missing. I've tried everything the other posts answers have suggested, but to no avail.
我不确定问题到底是什么,但我可以不带任何参数使用 get_terms
,并且我在我的网站上获取每个术语,但显然我想将其隔离到分类法宣布.我在循环外运行它,所以我不能传递 ID,也不想传递 ID.
I'm not sure what exactly the issue is, but I can use get_terms
without any parameters, and I get every term on my site, but clearly I want to isolate this to only the taxonomy declared. I am running this outside a loop, so I cannot pass an ID, nor do I want to pass an ID.
任何见解都有帮助!
我在使用 get_terms()
时遇到了这个问题.
I am getting this when using get_terms()
.
object(WP_Error)#992 (2) {
["errors"]=>
array(1) {
["invalid_taxonomy"]=>
array(1) {
[0]=>
string(17) "Invalid taxonomy."
}
}
["error_data"]=>
array(0) {
}
}
我的代码:
if ( ! function_exists( 'car_ctax_catalog' ) ) {
// Register Custom Taxonomy
function car_ctax_catalog() {
$labels = array(
'name' => _x( 'Categories', 'Taxonomy General Name', 'carmon' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'carmon' ),
'menu_name' => __( 'Category', 'carmon' ),
'all_items' => __( 'All Categories', 'carmon' ),
'parent_item' => __( 'Parent Category', 'carmon' ),
'parent_item_colon' => __( 'Parent Category:', 'carmon' ),
'new_item_name' => __( 'New Category Name', 'carmon' ),
'add_new_item' => __( 'Add New Category', 'carmon' ),
'edit_item' => __( 'Edit Category', 'carmon' ),
'update_item' => __( 'Update Category', 'carmon' ),
'view_item' => __( 'View Category', 'carmon' ),
'separate_items_with_commas' => __( 'Separate categories with commas', 'carmon' ),
'add_or_remove_items' => __( 'Add or remove categories', 'carmon' ),
'choose_from_most_used' => __( 'Choose from the most used', 'carmon' ),
'popular_items' => __( 'Popular Categories', 'carmon' ),
'search_items' => __( 'Search Categories', 'carmon' ),
'not_found' => __( 'Not Found', 'carmon' ),
'no_terms' => __( 'No categories', 'carmon' ),
'items_list' => __( 'Categories list', 'carmon' ),
'items_list_navigation' => __( 'Categories list navigation', 'carmon' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'ctax_catalog',
);
register_taxonomy( 'ctax_catalog', array( 'cpt_catalog' ), $args );
}
add_action( 'init', 'car_ctax_catalog', 0 );
}
获取条款
$args = array (
'post_type' => 'catalog',
'taxonomy' => 'ctax_catalog',
'hide_emoty' => false
);
$tax = array ( 'ctax_catalog' );
$terms = get_terms($tax, $args);
echo"<pre>";var_dump($terms);echo"</pre>";
编辑
我也尝试过,结果相同.
I've also tried this with the same result.
$terms = get_terms('ctax_catalog');
$tterms = get_terms( array ('taxonomy' => 'ctax_catalog' ) );
echo"<pre>";var_dump($terms);echo"</pre>";
echo"<pre>";var_dump($tterms);echo"</pre>";
推荐答案
我想出了以下骇人听闻的解决方法,直到找到更好的解决方案:
I came up with the following hackish work-around, until a better solution could be found:
$terms = get_terms();
$gradeOptions = "";
foreach ($terms as $term) {
if ($term->taxonomy === "pa_grade") {
$gradeOptions .= "<options value=\"{$term->term_id}\">{$term->name}</options>";
}
}
var_dump($gradeOptions);
die();
这篇关于get_terms() 返回无效的分类法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!