解决 PHP 7.2 环境中 WordPress 的 count() 调用问题
发布时间:2025-10-19 15:16:26 本站作者 【 字体:大 中 小 】 浏览:83 次
群里有小伙伴 at 我,然后问了一个 WordPress 的报错问题,看了一下报错内容。嗯,这家伙用的 PHP 肯定是 7.2 版本的,然后就问了一下,果不其然还真是。
Warning: count(): Parameter must be an array or an object that implements Countable in /wp-includes/media.php on line 1206
报错原因
首先来看一下 count() 的方法原型。其中 $array_or_countable 参数,需要是数组或者 Countable 对象(Countable 接口能让对象可以被用于count函数的能力)。在 PHP 7.2 中对于 count() 有一个新增的变更,具体可参考官网文档。
int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

也就是说在 7.2 版本中当无效的类型传递给 $array_or_countable 参数时,count() 会产生警告,所以我们可以知道,上面提到的报错应该是传了无效的类型给 count() 才导致发出的警告,不过这里还需要具体环境具体分析,接下来我们就看一下问题排查。
问题排查
在解决 WordPress 问题的时候,先把外因(插件、主题)排除,然后再看是不是 WordPress 自身的问题。这里先让其关闭了所有插件(排除插件导致),然后通过与小伙伴的交流,了解到了以下几点:
1、关闭所有插件之后问题依然存在;
2、用的主题是收费的,所以不方便调试;
3、切换主题之后,问题依然存在;
4、确定问题根源是用 WordPress 的手机 APP 上传的图片会出现这个问题;
解决问题
再看一下上面提到的报错位置 /wp-includes/media.php on line 1206 ,根据与小伙伴聊天排除了插件的问题,剩下主题和程序自身缺陷了,再确定了是用 WordPress 的手机 APP 上传的图片会出现这个问题,而且切换主题之后,问题依然存在。所以怀疑是程序自身的缺陷导致的这个问题,接下来我们就看一下 /wp-includes/media.php 在 1206 行有啥东西吧。
/**
* Filters an image's 'srcset' sources.
*
* @since 4.4.0
*
* @param array $sources {
* One or more arrays of source data to include in the 'srcset'.
*
* @type array $width {
* @type string $url The URL of an image source.
* @type string $descriptor The descriptor type used in the image candidate string,
* either 'w' or 'x'.
* @type int $value The source width if pAIred with a 'w' descriptor, or a
* pixel density value if paired with an 'x' descriptor.
* }
* }
* @param array $size_array Array of width and height values in pixels (in that order).
* @param string $image_src The 'src' of the image.
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id Image attachment ID or 0.
*/
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
// Only return a 'srcset' value if there is more than one source.
if ( ! $src_matched || count( $sources ) < 2 ) {
return false;
}看代码发现 1206 行在一个名叫 wp_calculate_image_srcset 的方法中(上面的点仅截取了部分重要代码和注释,问题代码在第 25 行),而且 count 调的 $sources 也是一个数组。那么只能说明在不知道哪里调用 wp_calculate_image_srcset 方法的时候,传过来的 $sources 是一个非数组,所以出现了这个问题,那么应该在调用 count 之前确保 $sources 是一个数组就成了,所以我们这里加一个判断 ! is_array( $sources ) 就可以了,所以修改之后的代码如下
/**
* Filters an image's 'srcset' sources.
*
* @since 4.4.0
*
* @param array $sources {
* One or more arrays of source data to include in the 'srcset'.
*
* @type array $width {
* @type string $url The URL of an image source.
* @type string $descriptor The descriptor type used in the image candidate string,
* either 'w' or 'x'.
* @type int $value The source width if paired with a 'w' descriptor, or a
* pixel density value if paired with an 'x' descriptor.
* }
* }
* @param array $size_array Array of width and height values in pixels (in that order).
* @param string $image_src The 'src' of the image.
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id Image attachment ID or 0.
*/
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
// Only return a 'srcset' value if there is more than one source.
if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) {
return false;
}战后总结
把改好的代码让群里的小伙伴改好之后,问题成功的解决了 ,之所以对这个 count() 报错定位快,是因为在制作 Dobby 主题的时候,也同样遇到了一个 count() 的问题,不过调用的函数是不一样的,当时发现问题之后就提了一个 Ticket ,答复在后期版本会兼容这个问题,所以大家如果在使用 WordPress 4.9.4 或者之前的版本使用 PHP7.2 遇到同样问题的话,可以参考这里的方法修复,然后安心等待官方的修复版本。
Office2019激活密钥,office2019专业版产品激活码,2026.5最新分享
Office2021密钥激活码,Office2021专业版产品激活密钥最新2026
office2010 产品密钥,office2010永久激活密钥最新分享包括图文教程
office 2021正式版激活密钥,office2021专业正式版产品激活码免费分享
Office2019激活密钥,office2019专业版产品激活码,2026.5最新分享

Office2021密钥激活码,Office2021专业版产品激活密钥最新2026

office2010 产品密钥,office2010永久激活密钥最新分享包括图文教程

office 2021正式版激活密钥,office2021专业正式版产品激活码免费分享

正版 office2019产品密钥,office2019专业版激活密钥 office2019 永久激活

Office2020专业版永久激活秘钥,office2020产品激活码(图文教程)

office2020产品激活密钥激活方法,office2020 永久激活码【转】

2026最新office2010激活密钥分享,office2010永久激活码产品激活密钥(附激活教程)

2026年5月更新,office2020 永久激活的激活密钥,office2020 产品激活密钥激活码(附激活教程)

Office2010产品密钥激活密钥,2026最新的Office2010产品密钥激活码有哪些?

