如何在Start_lvl函数中显示动态内容

时间:2016-09-06 作者:bakar

我正在创建一个自定义导航助行器,但在start\\u lvl函数中输出父菜单项标题时遇到问题。我想在<div class="dropdown"> 在下面的start\\u lvl函数中。

function start_lvl(&$output, $depth = 0, $args = array()) {

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

        if ($depth == 0) {

            $out_div = \' <div class="dropdown-wrapper"><div class="dropdown">This is where I want to output parent item title\';
        }
        else {
            $out_div = \'\';
        }
        // build html
        $output.= "\\n" . $indent . $out_div . \'<ul>\' . "\\n";
    }
这是完整的导航助行器代码。

class my_custom_navwalker extends Walker_Nav_Menu {

    function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
    {
        $id_field = $this->db_fields[\'id\'];
        if (is_object($args[0])) {
            $args[0]->has_children = !empty($children_elements[$element->$id_field]);
        }
        return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }

    function start_lvl(&$output, $depth = 0, $args = array()) {

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

        if ($depth == 0) {

            $out_div = \' <div class="dropdown-wrapper"><div class="dropdown">\';
        }
        else {
            $out_div = \'\';
        }
        // build html
        $output.= "\\n" . $indent . $out_div . \'<ul>\' . "\\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array() , $id = 0) {
        global $wp_query;

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

        $class_names = $value = \'\';
        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $class_names = join(\' \', apply_filters(\'nav_menu_css_class\', array_filter($classes) , $item));
        // $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';

        if ($args->has_children && $depth == 0) {
            $has_sub = \' has-sub\';
        }

        $output.= $indent . \'<li id="menu-item-\' . $item->ID . \'"\' . $value . \'class="\' . $class_names . $has_sub . \'">\';

        $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) . \'"\' : \'\';
        $prepend = \'\';
        $append = \'\';

        //$description = !empty($item->description) ? \'<div class="">\' . $item->description . \'</div>\' : \'\';
        // if($depth != 0)
        // {
        //         $description = $append = $prepend = "";
        // }


        $item_output = $args->before;

        $item_output.= \'<a\' . $attributes . \'>\';

        $item_output.= $args->link_before . $prepend . apply_filters(\'the_title\', $item->title, $item->ID) . $append;
        $item_output.= \'</a>\';
        //$item_output.= $description . $args->link_after;
        $item_output.= $args->after;
        $output.= apply_filters(\'walker_nav_menu_start_el\', $item_output, $item, $depth, $args, $id);
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\\t", $depth);
        if ($depth == 0) {
            $out_div_close = \'</div></div>\';
        }
        else {
            $out_div_close = \'\';
        }
        $output.= "$indent" . "\\n";

        $output.= "</ul>" . $out_div_close . "\\n";
    }
}
任何帮助都将不胜感激。

提前谢谢。

1 个回复
SO网友:bakar

最后,经过彻底的搜索,正如@Howdy\\u McGee在评论中所建议的那样,我让导航助行器按预期工作。对于那些正在寻找类似东西的人,以下是我是如何做到的。

而不是在中输出父菜单项标题start_lvl 函数,我将其包含在start_el 函数和最终代码如下所示:

class my_custom_navwalker extends Walker_Nav_Menu {

    function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
    {
        $id_field = $this->db_fields[\'id\'];
        if (is_object($args[0])) {
            $args[0]->has_children = !empty($children_elements[$element->$id_field]);
        }
        return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }

    function start_lvl(&$output, $depth = 0, $args = array()) {

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

        if ($depth == 0) {

            $out_div = \'\';
        }
        else {
            $out_div = \'\';
        }
        // build html
        $output.= "\\n" . $indent . $out_div . \'<ul>\' . "\\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array() , $id = 0) {
        global $wp_query;

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

        $class_names = $value = \'\';
        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $class_names = join(\' \', apply_filters(\'nav_menu_css_class\', array_filter($classes) , $item));
        // $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';

        if ($args->has_children && $depth == 0) {
            $has_sub = \' has-sub\';
        }

        $output.= $indent . \'<li id="menu-item-\' . $item->ID . \'"\' . $value . \'class="\' . $class_names . $has_sub . \'">\';

        $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) . \'"\' : \'\';
        $prepend = \'\';
        $append = \'\';

        //$description = !empty($item->description) ? \'<div class="">\' . $item->description . \'</div>\' : \'\';
        // if($depth != 0)
        // {
        //         $description = $append = $prepend = "";
        // }


        $item_output = $args->before;

        $item_output.= \'<a\' . $attributes . \'>\';

        $item_output.= $args->link_before . $prepend . apply_filters(\'the_title\', $item->title, $item->ID) . $append;
        $item_output.= \'</a>\';
        //$item_output.= $description . $args->link_after;


        if ( $args->has_children && $depth == 0) {

            $item_output .= \' <div class="dropdown-wrapper"><div class="dropdown"><div class="item-title-class">\'.$item->title.\'</div>\';

        }

        $item_output.= $args->after;
        $output.= apply_filters(\'walker_nav_menu_start_el\', $item_output, $item, $depth, $args, $id);
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\\t", $depth);
        if ($depth == 0) {
            $out_div_close = \'\';
        }
        else {
            $out_div_close = \'\';
        }
        $output.= "$indent" . "\\n";

        $output.= "</ul>" . $out_div_close . "\\n";
    }
}

相关推荐

Conditional secondary menus

我有一个由3个项目组成的主菜单,每个项目都有一个不同的辅助菜单。我想在每个页面上显示相关的二级菜单。我目前使用的是带有slug的is\\u页面。我需要添加wpml,这样每个页面都会有更多的slug。我想问一下,您是否可以建议我一个更干净、更易于维护的解决方案。这就是我当时所做的:if ( is_page( \'page-1\' ) ) { wp_nav_menu( array( \'theme_location\' => \'secondary-menu-