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

WordPress后台将Posts改为Articles时出现未定义数组键0警告

问题:WordPress修改Posts为Articles后出现PHP未定义数组键警告

用户使用一段2012年的代码将WordPress后台的“Posts”改为“Articles”,代码如下:

// Function to change "posts" to "Articles" in the admin side menu
function change_post_menu_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Articles';
    $submenu['edit.php'][5][0] = 'Articles';
    $submenu['edit.php'][10][0] = 'Add Article';
    $submenu['edit.php'][16][0] = 'Tags';
    echo '';
}
add_action( 'admin_menu', 'change_post_menu_label' );
// Function to change post object labels to "news"
function change_post_object_label() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'Articles';
    $labels->singular_name = 'Article';
    $labels->add_new = 'Add Article';
    $labels->add_new_item = 'Add Article';
    $labels->edit_item = 'Edit Article';
    $labels->new_item = 'Article';
    $labels->view_item = 'View Article';
    /* error reporting lines start here */
    $labels->search_items = 'Search Articles';
    $labels->not_found = 'No Articles found';
    $labels->not_found_in_trash = 'No Articles found in Trash';
}
/* error reporting lines end here */
add_action( 'init', 'change_post_object_label' );

执行后wp-admin错误日志出现多条PHP Warning: Undefined array key 0 in警告,集中在$labels->search_items及后续代码行,需明确问题原因及修复方案。


问题原因

  1. 代码兼容性过时:该代码针对2012年的旧版WordPress(3.x/4.x)编写,新版WordPress中,$wp_post_types['post']->labels的内部结构已发生变化,直接赋值部分标签属性会触发未定义索引警告。
  2. 硬编码菜单索引风险:原代码直接使用$menu[5]、$submenu['edit.php'][5]这类固定索引,若其他插件/主题修改了菜单顺序,会导致访问不存在的数组键。
  3. 钩子时机问题:init钩子执行时,部分标签可能尚未完成初始化,直接赋值会触发属性未定义的警告。

修复方案

使用WordPress官方推荐的现代钩子和写法,兼容新版WordPress且避免警告:

// 修改文章类型标签(官方推荐方式)
add_filter('register_post_type_args', 'change_post_type_labels', 10, 2);
function change_post_type_labels($args, $post_type) {
    if ($post_type === 'post') {
        $args['labels'] = array_merge($args['labels'], array(
            'name'                  => __('Articles', 'your-theme-text-domain'),
            'singular_name'         => __('Article', 'your-theme-text-domain'),
            'add_new'               => __('Add Article', 'your-theme-text-domain'),
            'add_new_item'          => __('Add Article', 'your-theme-text-domain'),
            'edit_item'             => __('Edit Article', 'your-theme-text-domain'),
            'new_item'              => __('Article', 'your-theme-text-domain'),
            'view_item'             => __('View Article', 'your-theme-text-domain'),
            'search_items'          => __('Search Articles', 'your-theme-text-domain'),
            'not_found'             => __('No Articles found', 'your-theme-text-domain'),
            'not_found_in_trash'    => __('No Articles found in Trash', 'your-theme-text-domain'),
            'menu_name'             => __('Articles', 'your-theme-text-domain')
        ));
    }
    return $args;
}

// 安全修改后台菜单标签(可选,上述menu_name已覆盖)
add_action('admin_menu', 'change_post_menu_label');
function change_post_menu_label() {
    global $menu;
    foreach ($menu as $key => $item) {
        if ($item[2] === 'edit.php') {
            $menu[$key][0] = __('Articles', 'your-theme-text-domain');
            break;
        }
    }
}

关键修复点说明

  • 使用register_post_type_args钩子:这是WordPress官方推荐的修改文章类型标签的方式,兼容所有新版WordPress,避免直接操作全局变量的风险。
  • 数组合并赋值:保留原有标签的默认值,仅覆盖需要修改的部分,避免遗漏或访问不存在的属性。
  • 遍历查找菜单:不再硬编码菜单索引,通过edit.php标识找到目标菜单,避免因菜单顺序变化导致的错误。
  • 加入文本域:支持多语言,符合WordPress开发规范(替换your-theme-text-domain为你的主题实际文本域)。

注意事项

  1. 修改前请备份functions.php文件,或使用子主题的functions.php进行修改,避免损坏主题。
  2. 若使用缓存插件,修改后需清除缓存确保生效。
  3. 测试后台各页面,确认所有“Posts”已替换为“Articles”且无警告出现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:53:26