我的自定义面包屑中出现了以下错误:注意:试图获取非对象的属性。
这是代码:
$taxonomy_exists = taxonomy_exists($custom_taxonomy);
if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {
$taxonomy_terms = get_the_terms( $post, $custom_taxonomy );
$cat_id = $taxonomy_terms[0]->term_id; // ERROR LINE
$cat_nicename = $taxonomy_terms[0]->slug; // ERROR LINE
$cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy); // ERROR LINE
$cat_name = $taxonomy_terms[0]->name;
}我怎么能解决这个问题?
发布于 2018-06-25 13:17:47
您需要检查get_the_terms()是否真的返回任何内容。如果一个帖子在分类法中没有术语,那么$taxonomy_terms实际上不会在其中包含任何术语。如果$taxonomy_terms是空的,那么$taxonomy_terms[0]实际上不是一个术语对象,所以尝试访问它上的属性(->term_id)会引发一个错误:
$taxonomy_exists = taxonomy_exists($custom_taxonomy);
if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {
$taxonomy_terms = get_the_terms( $post, $custom_taxonomy );
if ( ! empty( $taxonomy_terms ) ) {
$cat_id = $taxonomy_terms[0]->term_id; // ERROR LINE
$cat_nicename = $taxonomy_terms[0]->slug; // ERROR LINE
$cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy); // ERROR LINE
$cat_name = $taxonomy_terms[0]->name;
}
}https://wordpress.stackexchange.com/questions/306908
复制相似问题