3.级别的WordPress引导菜单

时间:2017-01-16 作者:Peronia

我使用bootstrap 3和导航栏作为导航。我发现创建菜单的代码很有魅力:https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/#comment-1609

但目前这段代码没有提供3。级别项目。

我发现这个页面显示了3。级别:http://blog.inbytes.de/2015/11/responsive-multilevel-dropdown-menue-mit-bootstrap-3/

你能帮我找出显示3的php部分吗。也要升级物品?

RegardsPeronia餐厅

编辑:我的代码:

function create_bootstrap_menu( $theme_location ) {
if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {

    $menu_list  = \'<nav class="navbar navbar-default">\' ."\\n";
    $menu_list .= \'<div class="container-fluid">\' ."\\n";
    $menu_list .= \'<div class="navbar-header">\' ."\\n";
    $menu_list .= \'<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">\' ."\\n";
    $menu_list .= \'<span class="sr-only">Toggle navigation</span>\' ."\\n";
    $menu_list .= \'<span class="icon-bar"></span>\' ."\\n";
    $menu_list .= \'<span class="icon-bar"></span>\' ."\\n";
    $menu_list .= \'<span class="icon-bar"></span>\' ."\\n";
    $menu_list .= \'</button>\' ."\\n";
    $menu_list .= \'</div>\' ."\\n";

    $menu_list .= \'<!-- Collect the nav links, forms, and other content for toggling -->\';


    $menu = get_term( $locations[$theme_location], \'nav_menu\' );
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    $menu_list .= \'<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">\' ."\\n";
    $menu_list .= \'<ul class="nav navbar-nav">\' ."\\n";

    foreach( $menu_items as $menu_item ) {
        if( $menu_item->menu_item_parent == 0 ) {

            $parent = $menu_item->ID;

            $menu_array = array();
//------------> here start the 2. lvl navigation
            foreach( $menu_items as $submenu ) {
                if( $submenu->menu_item_parent == $parent ) {
                    $bool = true;
                    $menu_array[] = \'<li><a href="\' . $submenu->url . \'">\' . $submenu->title . \'</a></li>\' ."\\n";
                }
            }
            if( $bool == true && count( $menu_array ) > 0 ) {

                $menu_list .= \'<li class="dropdown">\' ."\\n";
                $menu_list .= \'<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">\' . $menu_item->title . \' <span class="caret"></span></a>\' ."\\n";

                $menu_list .= \'<ul class="dropdown-menu">\' ."\\n";
                $menu_list .= implode( "\\n", $menu_array );
                $menu_list .= \'</ul>\' ."\\n";
                $menu_list .= \'</li>\' ."\\n";

            } else {
//------------> here ends the 2. lvl navigation 
//------------> here start the 1. lvl navigation 
                $menu_list .= \'<li>\' ."\\n";
                $menu_list .= \'<a href="\' . $menu_item->url . \'">\' . $menu_item->title . \'</a>\' ."\\n";
                $menu_list .= \'</li>\' ."\\n";
            }
//------------> here ends the 1. lvl navigation
//------------> the 3. lvl is missing
        }

    }

    $menu_list .= \'</ul>\' ."\\n";
    $menu_list .= \'</div><!-- /.navbar-collapse -->\' ."\\n";
    $menu_list .= \'</div><!-- /.container-fluid -->\' ."\\n";
    $menu_list .= \'</nav>\' ."\\n";

} else {
    $menu_list = \'<!-- no menu defined in location "\'.$theme_location.\'" -->\';
}

echo $menu_list;
}
正如我在评论中所写,3。缺少lvl导航。我不知道该在哪里添加。

1 个回复
SO网友:prosti

您只需要改进HTML模板。

你可以用这个来让它工作。您只需要改进HTMLlike this.

您也可以尝试multi-level-dropdown-menu.