我现在要哑了…
print_r($terms);
这个…吗
Array
(
[7] => stdClass Object
(
[term_id] => 7
[name] => Testwhatever
[slug] => testwhatever
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => event_type
[description] =>
[parent] => 0
[count] => 2
[object_id] => 8
)
)
如何print
鼻涕虫?我认为打印print($terms->slug)
应该完成这项工作,但它说:"试图获得非对象的属性"
更新:
function get_event_term($post) {
$terms = get_the_terms( (int) $post->ID, 'event_type' );
if ( !empty( $terms ) ) {
print_r($terms);
return $terms[7]->slug;
}
}
它是一个对象数组(即使它只包含索引"7"处的一个条目),而不是一个对象
echo $terms[7]->slug;
有多种
foreach ($terms as $term) echo $term->slug;
尝试
print_r($terms[7]->slug);
对象stdclass位于数组[7]偏移量中。
或者,让事情稍微复杂一点:
array_walk($terms, function($val,$key) use(&$terms){
var_dump($val->slug);
});
试试这个。
print ($terms[7]->slug);
print_r ($terms[7]->slug)
在我看来是合乎逻辑的,因为它是一个对象数组
更新
由于您不确定get_event_term
应返回多少项,因此应返回一个Array。
function get_event_term($post) {
$aReturn = array();
$terms = get_the_terms( (int) $post->ID, 'event_type' );
if ( !empty( $terms ) ) {
print_r($terms);
foreach($terms as $term){ // iterate through array of objects
$aReturn[] = $term->slug; // get the property that you need
}
}
return $aReturn;
}
函数使用wp_parse_args系统来管理其单个$args 工作对我来说很好更多细节 http://codex.wordpress.org/Function_Reference/wp_parse_args$args = wp_parse_args( $args, $term->name );
echo $arg[157]['term_id']; //output 157
echo $arg[157]['name']; //output Entertainment