【问题标题】:PHP walker class arrays and objectsPHP walker 类数组和对象
【发布时间】:2016-06-26 23:05:01
【问题描述】:

我正在使用 walker 类为 WordPress 主题创建菜单,我拥有的代码是;

class Walker_Nav_Primary extends Walker_Nav_menu {

    function start_lvl( &$output, $depth = 0, $args = array() ){ //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    }

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span

        $indent = ( $depth ) ? str_repeat("\t",$depth) : '';

        $li_attributes = '';
        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $classes[] = ($args->walker->has_children) ? 'dropdown' : '';
        $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
        $classes[] = 'menu-item-' . $item->ID;
        if( $depth && $args->walker->has_children ){
            $classes[] = 'dropdown-submenu';
        }

        $class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="' . esc_attr($class_names) . '"';

        $id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';

        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr($item->url) . '"' : '';

        $attributes .= ( $args->walker->has_children ) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';

        $item_output = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= ( $depth == 0 && $args->walker->has_children ) ? ' <b class="caret"></b></a>' : '</a>';
        $item_output .= $args->after;

        $output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

    }

}

但是,当一位朋友查看主题时,他看到了下面的消息,我和其他人都没有看到此消息,并且菜单工作正常。

严格的标准:Walker_Nav_Primary::start_lvl() 的声明 应该与 Walker_Nav_Menu::start_lvl(&$output,$depth = 0, $args = Array) in .../inc/walker.php 第 0 行

所以他做了一些改变,第一个是通过在 start_lvl 函数中放入 $args =array() 来解决这个错误,但后来他写道 $args 不是一个对象,它是一个数组,所以你can't do $args->something, you have to do $args['something'], 给出代码;

class Walker_Nav_Primary extends Walker_Nav_menu {

    function start_lvl( &$output, $depth = 0, $args = array() ) { //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    }

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
        $indent = ( $depth ) ? str_repeat("\t",$depth) : '';
        $li_attributes = '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $classes[] = ($args['walker']->has_children) ? 'dropdown' : '';

        $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
        $classes[] = 'menu-item-' . $item->ID;
        if( $depth && $args['walker']->has_children ){
            $classes[] = 'dropdown-submenu';
        }
        $class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="' . esc_attr($class_names) . '"';
        $id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr($item->url) . '"' : '';
        $attributes .= ( $args['walker']->has_children ) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
        $item_output = $args['before'];
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args['link_before'] . apply_filters( 'the_title', $item->title, $item->ID ) . $args['link_after'];
        $item_output .= ( $depth == 0 && $args['walker']->has_children ) ? ' <b class="caret"></b></a>' : '</a>';
        $item_output .= $args['after'];
        $output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

但是,当我运行代码时,出现以下错误

致命错误:不能将 stdClass 类型的对象用作数组

指的是行

$classes[] = ($args->walker->has_children) ? 'dropdown' : '';

已改为

$classes[] = ($args['walker']->has_children) ? 'dropdown' : '';

我们都使用相同的 PHP 和 WordPress,所以我不明白为什么它对他有用,而不是我,这是正确的方法。

谁能解释一下?

【问题讨论】:

  • 好的,首先你必须确定是 $args 数组还是对象?或者不一致,然后您必须确定是否有任何名为walker 的键或属性,然后您应该检查键或属性是否为对象。我会回答给你最好的用法,但你必须确定是 $args 数组还是对象。

标签: php arrays wordpress


【解决方案1】:

如果您确定$args 是一个数组,请将此代码放在使用$args 之前

if (!isset($args['walker']) || !(is_object($args['walker']) && isset($args['walker']->has_children)) {
   $classes[] = '';
   $args = (object) array('walker' => array('has_children' => false));
}

但是如果$args 是一个对象,那么使用以下:

if (!isset($args->walker) || !(is_object($args->walker) && isset($args->walker->has_children)) {
   $classes[] = '';
   $args = (object) array('walker' => array('has_children' => false));
}

如果不一致先让这个一致,一定要$args数组或者对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2015-06-28
    • 2019-07-31
    • 2011-12-10
    • 2017-09-09
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多