修复PHP7.2以上版本Count()错误

最近因为更新wordpress5.2 经常出现代码报错,所以打开了DEBUG,在文章页面上出现了Warning: count(): Parameter must be an array or an object that implements Countable in post-template.php 310 行错误。 我不是技术人员,但不希望错误,Google查阅后,自己修改后就不再报错了。

以下为记录:

  • 找到wordpress下的post-template.php文件,并用notepad++打开,或其它代码软件也行
  • 找到第310行,更换代码如下

旧代码

if ( $elements['page'] > count( $elements['pages'] ) ) { // if the requested page doesn't exist
$elements['page'] = count( $elements['pages'] ); // give them the highest numbered page that DOES exist
}

新代码

if ( is_array($elements['page']) && $elements['page'] > count( $elements['pages'] ) ) { // if the requested page doesn't exist
$elements['page'] = count( $elements['pages'] ); // give them the highest numbered page that DOES exist
}

Previous/Next

Say Something!

Leave a Reply