WPML CMS导航和自定义导航未定义属性通知

时间:2017-02-13 作者:rhand

我有一门Nav\\u Walker课程来学习超级鱼菜单。而且效果很好。现在,我添加了WPML CMS Nav,但我收到了如下通知:

notice: Undefined property: wpml_ls_menu_item::$current in /srv/www/sub.domain.com/current/web/app/themes/theme/lib/setup.php on line 313
在那条线上有

$element->classes[] = ( $element->current || $element->current_item_ancestor ) ? \'active\' : \'\';
它还提到$element->current_item_ancestor 也未定义为属性。在类内执行var\\u转储时:

var_dump($element->current_item_ancestor);
我确实得到布尔值false。

/srv/www/sub.domain.com/current/web/app/themes/theme/lib/setup.php:314:boolean false
这是第二个未定义属性的通知。

这是因为display_element 是一个私有方法,我不应该这样(直接)调用它?我该如何补救?

以下为完整课程:

class Nav_Walker extends \\Walker_Nav_Menu
{

    function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
    {
        $element->has_children = ! empty($children_elements[$element->ID]);
        $element->classes[] = ( $element->current || $element->current_item_ancestor ) ? \'active\' : \'\';
        //var_dump($element->current_item_ancestor);
        $element->classes[] = ( $element->has_children ) ? \'has-children\' : \'\';
        parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }
    /*
     * At the start of each element, output a <li> and <a> tag structure.
     *
     * Note: Menu objects include url and title properties, so we will use those.
     * @see Walker::start_el()
     */
    function start_el(&$output, $object, $depth = 0, $args = array(), $current_object_id = 0)
    {

        $item_html = \'\';
        $indent = ( $depth > 0  ? str_repeat(\'\', $depth) : \'\' ); // code indent
        $line = ( $depth < 2  ? "\\n" : \'\' ); // new line
        $this->curItem = $object;
        parent::start_el($item_html, $object, $depth, $args);
        $curItem = $object;
        $classes = empty($object->classes) ? array() : (array) $object->classes;

        if (in_array(\'current-menu-item\', $classes)) {
            $classes[] = \'current \';
        }

        $class_names = esc_attr(implode(\' \', apply_filters(\'nav_menu_css_class\', array_filter($classes), $object)));

        // Build html markup
        $output .= $indent .\'<li id="nav-menu-item-\'. $object->ID . \'" class="\' . $class_names . \'">\'   .   $line;

        // link attributes
        $attributes  = ! empty($object->attr_title) ? \' title="\'  . esc_attr($object->attr_title) .\'"\' : \'\';
        $attributes .= ! empty($object->target)     ? \' target="\' . esc_attr($object->target) .\'"\' : \'\';
        $attributes .= ! empty($object->xfn)        ? \' rel="\'    . esc_attr($object->xfn) .\'"\' : \'\';
        $attributes .= ! empty($object->url)        ? \' href="\'   . esc_attr($object->url) .\'" itemprop="url"\' : \'\';
        $attributes .= ( $object->has_children )      ? \' aria-haspopup="true"\' : \'\';

        $item_html = sprintf(
            \'%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s\',
            $args->before,
            $attributes,
            $args->link_before,
            apply_filters(\'the_title\', $object->title, $object->ID),
            $args->link_after,
            $args->after
        );

        // Complete the html markup by apply filter so other plugins can hook into it.
        $output .= apply_filters(\'walker_nav_menu_start_el\', $item_html, $object, $depth, $args);
    }
}

1 个回复
最合适的回答,由SO网友:rhand 整理而成

使用isset检查变量以查看其是否包含值:

$element->classes[] = ( isset($element->current) || isset($element->current_item_ancestor )) ? \'active\' : \'\';
这解决了通知问题。这似乎起到了作用。我想我想得太多了。现在,我可以开始添加一些阿拉伯语内容,并看到开始使用语言切换器。