如何为get_terms函数解决WP_Error对象?
我在tax_alone_event
文件中注册了自定义分类法coustom-post-type.php
。
并包含在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');
答案 0 :(得分:2)
{ columnDefs: [{
"targets": 2,
"name": "winding temp",
"render": function (data, type, row, meta) {
var hexvalue =base64toHEX(data);
var deviceid= hexToDec(hexvalue.substr(1,1));
var oil_temp= hexToDec(hexvalue.substr(2,2));
var winding_temp= hexToDec(hexvalue.substr(4,2));
return 'Winding Temp: ' + winding_temp ;
}
},{
"targets": 1,
"name": "Oil temp",
"render": function (data, type, row, meta) {
var hexvalue =base64toHEX(data);
var deviceid= hexToDec(hexvalue.substr(1,1));
var oil_temp= hexToDec(hexvalue.substr(2,2));
var winding_temp= hexToDec(hexvalue.substr(4,2));
return 'oil temp: ' + oil_temp ;
}
},
{
"targets": 0,
"name" : "Timestamp",
"render": function (data, type, row, meta) {
var updDate= UtcToIst(data).toLocaleDateString();
var updTime= UtcToIst(data).toLocaleTimeString();
var updDateTime=updDate + ", " + updTime;
return updDateTime ;
}
}
],
data: Readings,
columns: [{
"data": "timestamp"
}, {
"data": "dataFrame"
},
{
"data": "dataFrame"
}
]
函数将仅显示默认的get_terms()
,除非您将其挂接到init。
WordPress taxonomies
输出:
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');
答案 1 :(得分:1)
您应该通过4.5.0-https://developer.wordpress.org/reference/functions/get_terms/中$ args数组中的'taxonomy'参数传递分类法。
function get__terms_list() {
$terms = get_terms( array( 'taxonomy' => 'tax_alone_event' ) );
return $terms ;
}
print_r(get__terms_list());
答案 2 :(得分:1)
您可以通过两种方式消除错误
init
钩子,直接调用Alone_setup_post_type_project()
; get_terms
函数和init钩子因为您正在做的是在{word}向系统注册您的帖子类型和分类法之前无法使用get_terms
函数。
答案 3 :(得分:0)
我将您的代码复制给自己,效果很好。
您在哪个文件中有此部分?
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());
,以查看您的分类法是否已注册。