function im_check_term($name,$tax){
$term = get_term_by("name", $name,$tax);
return !is_wp_error($term) ? $term->term_id : false;
}注意:尝试在第11行的/home/pcodecom/demo.p30code.com/multimedia-2/wp-content/plugins/imdb/imdb.php中获取非对象的属性。
发布于 2020-04-04 08:08:29
如果您查看文献资料 for get_term_by(),您将看到它:
如果不存在
$taxonomy或未找到$term,将返回false。
您需要在代码中通过检查$term的值来说明这种可能性。您还将从文档中注意到,get_term_by()不返回WP_Error,因此is_wp_error()并不有用。这就是你需要的:
$term = get_term_by( 'name', $name, $tax );
return $term ? $term->term_id : false;您所看到的具体错误是因为如果$term是false,那么$term->term_id就是无效代码。
发布于 2020-04-04 08:09:37
get_term_by()可能返回布尔值false。因此,对WP_Error的简单检查是不够的。使用instanceof操作符代替,并测试WP_Term对象:
function im_check_term($name,$tax){
$term = get_term_by("name", $name,$tax);
return ( $term instanceof WP_Term ) ? $term->term_id : false;
}https://wordpress.stackexchange.com/questions/363193
复制相似问题