解决wordpress主题Fastos1.5.6使用出现Warning:Trying to access array offset on value of type bool in 556

问题

问题内容

在使用新的wordpress主题的时候出现了错误,如下图所示:

在这里插入图片描述
Warning : Trying to access array offset on value of type bool in /www/wwwroot/blog.solye.cn/wp- content/themes/fasto/inc/functions/theme.php on line 556

问题其实很简单,就是尝试在bool类型的数据上使用数组下标去访问。

问题定位

先在浏览器中定位对应的标签
在这里插入图片描述

在这里插入图片描述
然后再到警告的php文件/www/wwwroot/blog.solye.cn/wp- content/themes/fasto/inc/functions/theme.php556行处中寻找问题。
在这里插入图片描述

问题查询

一看这里访问下标值就感觉有问题,然后就去查看这个变量的赋值函数wp_get_attachment_image_src的返回值。

$post_thumbnail_arr = wp_get_attachment_image_src( 
	get_post_thumbnail_id( $post->ID ), 
	'full-single' );

在这里插入图片描述
官方的函数地址:
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

然后就发现如果没有图片信息就会直接返回false,这就导致上面通过数组索引值或者下标去访问出现问题,因为它是一个布尔值,没有索引。

解决办法

解决办法就是对其进行一个if else判断,

if($post_thumbnail_arr){
    
    
	$post_thumbnail = $post_thumbnail_arr[0];
}
else{
    
    
	$post_thumbnail = NULL;
}

因为后面的链接分享当中用到了上面的变量地址(0返回的值url),所以直接赋值为null,就不会出错了。

然后布局和显示就正常了。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44394801/article/details/118737998
今日推荐