1

我在 Wordpress 上有一个网站,我团队中的某个人更新了一些内容,现在该网站上有一个错误:

Warning: Illegal string offset 'output_key' in /.../wp-includes/nav-menu.php

我禁用了调试消息,现在该站点运行良好。我想这不是最好的解决方案。几个小时的谷歌搜索和在这里寻找东西让我意识到我必须告诉脚本这'output_key'是一个数组元素。


$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
        'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
    $args = wp_parse_args( $args, $defaults );
    $args['include'] = $items;


if ( ARRAY_A == $args['output']) {
        $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
        usort($items, '_sort_nav_menu_items');
        $i = 1;
        foreach( $items as $k => $item ) {
            $items[$k]->$args['output_key'] = $i++; //here is the error
        }
    }

我试图询问$args它是否是第 1 行中的数组。

$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
        'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
    $args = wp_parse_args( $args, $defaults );
    $args['include'] = $items;


if ( ARRAY_A == $args['output'] && is_array($args) ) {
        $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
        usort($items, '_sort_nav_menu_items');
        $i = 1;
        foreach( $items as $k => $item ) {
            $items[$k]->$args['output_key'] = $i++; //here is the error
        }
    }

但是第 6 行仍然在网站上显示错误。我不知道该怎么处理那条线。

4

1 回答 1

7

看来您正在使用不再与 PHP 7 兼容的过时版本的 wordpress。

请参阅关于变量处理的从 php 5.6 迁移到 php 7 部分

变量处理

这意味着表达式$items[$k]->$args['output_key']被解释为:

  • $items[$k]->{$args['output_key']}在 PHP 5 中
  • ($items[$k]->$args)['output_key']在 PHP 7 中

最近的 wordpress 代码似乎也解决了这个问题。

要手动修复问题,只需替换代码:

$items[$k]->$args['output_key'] = $i++; // here is the error

和:

$items[$k]->{$args['output_key']} = $i++; // problem solved :)

您还应该考虑升级整个 wordpress 安装,以确保其余代码与 PHP7 兼容。

于 2020-01-29T15:22:19.177 回答