简体   繁体   English

php通知(试图获取非对象的属性)错误

[英]php notice (trying to get property of non-object) error

So, I have following php for wp: 所以,我有关于wp的php:

 $usersNames = array();
 foreach ($userIDs as $userId) {
            $userInfo = get_userdata($userId);
            $usersNames[] = $userInfo->display_name; //this one                                     
        } 

I am getting an error for $usersNames . 我收到$usersNames的错误。

"PHP Notice:  Trying to get property of non-object in /functions.php on line"

What is going on? 到底是怎么回事?

Any suggestions? 有什么建议么?

Thanks 谢谢

EDIT: 编辑:

So, for $userIDs , I have an array of user ids. 因此,对于$userIDs ,我有一个用户ID数组。 Then I am trying to get user display name etc for individual ids. 然后我试图获得个人ID的用户显示名称等。

Your function get_userdata() will return False on failure, WP_User object on success. 你的函数get_userdata()False on failure, WP_User object on success.时返回False on failure, WP_User object on success.

And error Trying to get property of non-object means that this function has returned false . 和错误Trying to get property of non-object意味着此函数返回false

Simply check if $userInfo is not empty 只需检查$userInfo是否为空

$userInfo = get_userdata($userId);

if (!empty($userInfo)) {
    $usersNames[] = $userInfo->display_name;
}

you need judge whether $userInfo return the right obj 你需要判断$ userInfo是否返回正确的obj

  $usersNames = array(); foreach ($userIDs as $userId) { $userInfo = get_userdata($userId); $usersNames[] = isset($userInfo->display_name)?$userInfo->display_name:''; //this one } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM