boxmoe_header_banner_img

Hello! 欢迎来到盒子萌!

加载中

文章导读

wordpress使用PHP7.2后报错Warning count(): …post-template.php…


avatar
专收爆米花 2019年4月18日 4.5k

最近升级了php到7.2版本了,但是出现了个别页面有报错
Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/wp-includes/post-template.php on line 293

查看加百度后发现php7.2版本更新当传递一个无效参数的时候,count()函数会抛出warning的警告
也就是不要传递无效的参数,需要加判断

最终解决就是把count数组定义为0解决,wordpress官方还未更新,只能自己手动改下post-template.php

NOTE:将原来约293行的代码
if ( $page > count( $pages ) ) { // if the requested page doesn't exist
		$page = count( $pages ); // give them the highest numbered page that DOES exist
	}
NOTE:修改为
if ( is_array( $pages ) ) {
if ( $page > count( $pages ) ) // if the requested page doesn’t exist
$page = count( $pages ); // give them the highest numbered page that DOES exist
} else {
$page = 0;
}


评论(3)

查看评论列表
评论头像
风执行 2019年05月23日
## 分享一个优雅的写法 $page = is_countable($pages) && $page > count($pages) ? count($pages) : 0 ;
评论头像
专收爆米花 博主 2019年05月28日
太优雅了
评论头像
zklcdc 2020年04月07日
博主大大,更新到 5.4 后,$page = 0; 可能要让这个值等于 1,不然有些自定义页面的内容会消失

发表评论

表情 颜文字
插入代码