Php 为什么最新Wordpress更新时出现未定义属性stdClass错误?
更新到最新的Wordpress core 5.2.3后,我在一个自定义插件文件中收到这些错误消息Php 为什么最新Wordpress更新时出现未定义属性stdClass错误?,php,wordpress,Php,Wordpress,更新到最新的Wordpress core 5.2.3后,我在一个自定义插件文件中收到这些错误消息 Notice: Undefined property: stdClass::$addressLocality in /var/www/html/www.clientsite.com/wp/wp-content/plugins/hove- widgets/widgets/careers.php on line 52 Notice: Undefined property: stdClass::$ad
Notice: Undefined property: stdClass::$addressLocality in
/var/www/html/www.clientsite.com/wp/wp-content/plugins/hove-
widgets/widgets/careers.php on line 52
Notice: Undefined property: stdClass::$addressRegion in
/var/www/html/www.clientsite.com/wp/wp-content/plugins/hove-
widgets/widgets/careers.php on line 53
Notice: Undefined property: stdClass::$addressLocality in
/var/www/html/www.clientsite.com/wp/wp-content/plugins/hove-
widgets/widgets/careers.php on line 52
Notice: Undefined property: stdClass::$addressRegion in
/var/www/html/www.clientsite.com/wp/wp-content/plugins/hove-
widgets/widgets/careers.php on line 53
我查看了文件,但我无法确定为什么它声称该财产未定义。错误消息来自以下文件:
<?php
function hove_get_careers(){
$output = '';
$response = file_get_contents('https://hire.withgoogle.com/v2/api/t/hovecom/public/jobs');
$response = json_decode($response);
usort($response,function($a,$b) {return strnatcasecmp($a->hiringOrganization->department->name,$b->hiringOrganization->department->name);});
// /var_dump($response);die;
$department=null;
$count=0;
foreach($response as $r){
$title = $r->title;
$url = $r->url;
$addressLocality = !empty($r->jobLocation->address->addressLocality) ? $r->jobLocation->address->addressLocality : '';
$addressRegion = !empty($r->jobLocation->address->addressRegion) ? $r->jobLocation->address->addressRegion : '';
$location = $addressLocality.','.$addressRegion;
$actualdepartment = $r->hiringOrganization->department->name;
if($actualdepartment!=$department){
$count++;
if($count!=1){
$output .= '</ul>';
}
$department=$actualdepartment;
$output .= '<h3>'.$actualdepartment.'</h3>';
$output .= '<ul class="careers">';
}
$output .= '<li class="press-job"><span class="details"><span class="title">'.$title.'</span><span class="location">'.$location.'</span></span><span class="cta">Learn More</span><a href="'.$url.'" class="ab-link"></a></li>';
}
$output .= '</ul>';
return $output;
}
问题在于,代码只检查AddressLocation
和addressRegion
是否为空。它不会检查它们是否实际可用。可能插件使用的API在某个时候发生了变化,并且说属性现在要么不再可用,要么只有在满足某些条件时才提供
要修复此通知,我们只需检查是否使用函数设置了这两个属性,如下所示:
$addressLocality = isset($r->jobLocation->address->addressLocality) && !empty($r->jobLocation->address->addressLocality) ? $r->jobLocation->address->addressLocality : '';
$addressRegion = isset($r->jobLocation->address->addressRegion) && !empty($r->jobLocation->address->addressRegion) ? $r->jobLocation->address->addressRegion : '';
以下内容超出了您问题的原始范围,但我想指出:如果$AddressLocation
和$addressRegion
为空,则$location
将只是,
,这可能不是理想的输出。我将添加此附加检查以确保$location
为空或显示实际地址:
$location = ($addressLocality && $addressRegion) ? $addressLocality.','.$addressRegion : '';
代码中的第52行和第53行是什么?我猜是变量“<代码> $AddiSsListIs< /Calp>和<代码> $AddiSsReals<代码>,这是正确的吗?是的,这是正确的。请检查我的答案,如果它帮助考虑的话。如果您有任何问题,请毫不犹豫地提问。谢谢,但这似乎无法解决问题。服务器管理员已隐藏错误消息。我只能看到这页的格式有问题。我将尝试返回错误消息,以查看是否有任何更改。