admin管理员组文章数量:1391999
function im_add_new_term($name,$tax){
if($tax == "genre"){
return wp_insert_term($name,$tax,array("slug"=>array_search ($name, $genres)))["term_id"];
} else {
return wp_insert_term($name,$tax)["term_id"];
}
}
Fatal error: Cannot use object of type WP_Error as array in /home/pcodecom/demo.p30code/multimedia-2/wp-content/plugins/imdb/imdb.php on line 11
function im_add_new_term($name,$tax){
if($tax == "genre"){
return wp_insert_term($name,$tax,array("slug"=>array_search ($name, $genres)))["term_id"];
} else {
return wp_insert_term($name,$tax)["term_id"];
}
}
Share Improve this question edited Apr 4, 2020 at 8:10 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Apr 4, 2020 at 6:30 aliali 456 bronze badges 5 |Fatal error: Cannot use object of type WP_Error as array in /home/pcodecom/demo.p30code/multimedia-2/wp-content/plugins/imdb/imdb.php on line 11
1 Answer
Reset to default 1What's $genres
? I don't see it defined anywhere.
And wp_insert_term()
may return an error, so make sure to check if it is an error. So instead of simply doing return wp_insert_term($name,$tax)["term_id"]
, you could do something like this:
$data = wp_insert_term( $name, $tax );
if ( ! is_wp_error( $data ) ) {
return $data['term_id'];
}
本文标签: wp errorCannot use object of type WPError
版权声明:本文标题:wp error - Cannot use object of type WP_Error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744599171a2614963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Cannot use object of type WP_Error as array
means you cannot call it with array notation like$something['term_id']
, instead, you need to call it with arrow notation like$someting->term_id
– 西門 正 Code Guy - JingCodeGuy Commented Apr 4, 2020 at 6:37wp_insert_term($name,$tax)->term_id
ok? – ali Commented Apr 4, 2020 at 6:47function im_add_new_term($name,$tax){ if($tax == "genre"){ $ali = wp_insert_term($name,$tax,array("slug"=>array_search ($name, $genres))); return $ali->term_id; } else { $faraji = wp_insert_term($name,$tax); return $faraji->term_id; } }
not working – ali Commented Apr 4, 2020 at 7:40