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

请求修正nav-menu.php中Illegal string offset 'output_key'报错代码

Fixing the "Illegal string offset 'output_key'" Warning in WordPress nav-menu.php

Hey there, let's break down this warning and fix it properly! That error pops up because the code is trying to use $args['output_key'] as a property name for the menu item object, but something's off with that value—either $args isn't an array, the output_key key doesn't exist in it, or the value isn't a valid string that can be used as an object property.

Here's a robust, error-free rewrite of that line with safety checks to prevent the warning:

// Validate the $args array and output_key before using it
if (is_array($args) && isset($args['output_key']) && is_string($args['output_key'])) {
    // Use curly braces to clearly denote the variable property name
    $items[$k]->{$args['output_key']} = $i++;
} else {
    // Fallback to a default property name if the required key is missing/invalid
    $default_output_key = '_menu_item_index';
    $items[$k]->{$default_output_key} = $i++;
}

Why this works:

  • is_array($args): Makes sure we're not trying to access a key on a non-array value (like a string or null)
  • isset($args['output_key']): Checks that the output_key key actually exists in the $args array
  • is_string($args['output_key']): Ensures the value is a valid string that can be used as an object property name
  • Curly braces {$args['output_key']}: Eliminates any parsing ambiguity when using a variable as an object property (PHP sometimes gets confused without them)

If you're confident that $args should always have a valid output_key string (maybe this is a one-off edge case), you could simplify it to just add the curly braces:

$items[$k]->{$args['output_key']} = $i++;

But adding the full safety checks is better for long-term stability, especially if other plugins or themes might modify the navigation menu arguments.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:32:30