You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级PHP至8.0后出现WordPress数组偏移访问警告的修复求助

修复WordPress PHP8.0下的布尔值数组偏移警告

问题说明

将PHP从7.4升级到8.0后,WordPress出现警告:在/wp-includes/theme.php文件第2241行尝试对布尔类型值进行数组偏移访问,涉及代码片段:

if ( is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) {
    $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) );
}

问题原因

PHP8.0对类型检查更严格,禁止直接对布尔值进行数组偏移访问。当主题通过add_theme_support( 'post-thumbnails' )不带任何参数启用特色图像时,$_wp_theme_features['post-thumbnails']会被设置为布尔值true,而非数组结构,此时代码中访问$_wp_theme_features['post-thumbnails'][0]就会触发警告。

修复方案

方案1:临时修改核心文件(注意:WordPress更新会覆盖此修改)

打开/wp-includes/theme.php文件,定位到第2241行的代码块,增加对$_wp_theme_features['post-thumbnails']的数组类型检查:

if ( is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) && is_array( $_wp_theme_features['post-thumbnails'] ) ) {
    $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) );
}

方案2:使用主题钩子修正(推荐,不修改核心文件)

在当前主题的functions.php文件末尾添加以下代码:

add_action( 'after_setup_theme', function() {
    global $_wp_theme_features;
    // 将布尔值的post-thumbnails转换为包含所有公开文章类型的数组
    if ( isset( $_wp_theme_features['post-thumbnails'] ) && is_bool( $_wp_theme_features['post-thumbnails'] ) ) {
        $_wp_theme_features['post-thumbnails'] = [ get_post_types( ['public' => true] ) ];
    }
}, 11 );

这段代码会在主题初始化完成后执行,把布尔类型的post-thumbnails配置转换为标准数组结构,从根源避免类型错误。

方案3:升级WordPress到最新稳定版

如果你的WordPress版本较旧,官方大概率已在后续版本中修复了这个PHP8.0兼容性问题,直接升级到最新版即可解决。

内容的提问来源于stack exchange,提问作者Tausif Anwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 13:32:15