首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bootstrap 4 navwalker -多级菜单

Bootstrap 4 navwalker -多级菜单
EN

Stack Overflow用户
提问于 2018-07-18 17:40:46
回答 2查看 6.5K关注 0票数 5

我在我的Wordpress项目中使用了this nav walker。例如,我想创建多级菜单。我需要做些什么才能让它正常工作?或者它可能是另一个支持多级菜单的BS4 walker?

代码语言:javascript
运行
复制
Menu Item
- Sub menu
-- Sub menu item
-- Sub menu item
Menu Item
EN

回答 2

Stack Overflow用户

发布于 2018-10-03 05:22:14

  1. 添加此css

ul.dropdown-menu li > ul.dropdown-menu{ left: 100%;top: 0;} ul.dropdown-menu li:hover > ul.dropdown-menu,ul.dropdown-menu li:focus > ul.dropdown-menu{ display: block }

  • && 0 === $depth -wp-bootstrap-navwalker.php中删除以下代码引导

原始代码:

如果( isset( $args->has_children ) && $args->has_children && 0 === $depth && $args->depth >1){

编辑完成后:

如果( isset( $args->has_children ) && $args->has_children && $args->depth >1) {

票数 6
EN

Stack Overflow用户

发布于 2019-01-31 01:21:41

Bulding simple menu list -在这里的用户贡献笔记(第一个)中,你可以找到使用菜单数组数据构建菜单的代码,它很简单,可以根据需要进行修改。

代码如下:

代码语言:javascript
运行
复制
<?php
// Intented to use bootstrap 3.
// Location is like a 'primary'
// After, you print menu just add create_bootstrap_menu("primary") in your 
preferred position;

#add this function in your theme functions.php

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 .= '<!-- Brand and toggle get grouped for better mobile display -->' ."\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 .= '<a class="navbar-brand" href="' . home_url() . '">' . get_bloginfo( 'name' ) . '</a>';
    $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();
            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";

            } else {

                $menu_list .= '<li>' ."\n";
                $menu_list .= '<a href="' . $menu_item->url . '">' . $menu_item->title . '</a>' ."\n";
            }

        }

        // end <li>
        $menu_list .= '</li>' ."\n";
    }

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

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

echo $menu_list;
}
?>
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51398685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档