简体   繁体   English

将 Bootstrap 导航栏转换为 WordPress 菜单

[英]Convert Bootstrap navbar to WordPress menu

I know there are a lot of topics about this on the net but I find them very complicated.我知道网上有很多关于这个的话题,但我发现它们非常复杂。 Basically I want to convert a Bootstrap navigation menu to a WordPress Menu.基本上我想将 Bootstrap 导航菜单转换为 WordPress 菜单。

Say I have the default Bootstrap Navbar:假设我有默认的 Bootstrap 导航栏:

<nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

The way I would normally link this navbar with my WordPress pages is instead of manually listing each <li> I would use the following:我通常将此导航栏与我的 WordPress 页面链接的方式是,我将使用以下内容,而不是手动列出每个<li>

    <ul class="nav navbar-nav">
     <?php wp_list_pages('title_li=');?>
     </ul>

The output of this would list all my pages I created in WordPress:输出将列出我在 WordPress 中创建的所有页面:

 <ul class="nav navbar-nav">
    <li class="page_item page-item-9"><a href="...">About</a></li>
    <li class="page_item page-item-2"><a href="...">Sample Page</a></li>
  </ul>

This is all fine as I can add a page and it gets included in my menu as expected.这一切都很好,因为我可以添加一个页面,它会按预期包含在我的菜单中。

The problem问题

The problem is I don't know how to include a dropdown item in the menu bar and integrate that into WordPress, for example the dropdown item:问题是我不知道如何在菜单栏中包含下拉项并将其集成到 WordPress 中,例如下拉项:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
     <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
     </ul>
  </li>

How would I integrate this with WordPress so next time if I want to add a new dropdown list I can easily do it the same way as the normal menu items?我如何将它与 WordPress 集成,以便下次如果我想添加一个新的下拉列表,我可以像普通菜单项一样轻松地做到这一点?

I would appreciate it if you don't provide links to WordPress codex websites and other tutorials as I have already tried many things如果您不提供指向 WordPress codex 网站和其他教程的链接,我将不胜感激,因为我已经尝试了很多东西

You need to use this https://github.com/twittem/wp-bootstrap-navwalker , add the nav walker file and follow the instructions.你需要使用这个https://github.com/twittem/wp-bootstrap-navwalker ,添加 nav walker 文件并按照说明进行操作。 Here's a sample from a random site I made, I'm not adjusting it to your own site because you'll need to learn this for all your future WP developments.这是我制作的一个随机站点的示例,我不会将其调整到您自己的站点,因为您需要在未来的所有 WP 开发中学习这一点。 It's incredible easy, check it out:这非常简单,请查看:

    <div id="nav">
    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
      <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="<?php bloginfo('url'); ?>"><img class="logo" src="<?php bloginfo('template_directory'); ?>/images/logo.png" alt="" /></a>
        </div>


        <?php
        wp_nav_menu( array(
                'menu'              => 'primary',
                'depth'             => 2,
                'container'         => 'div',
                'container_class'   => 'navbar-collapse collapse',
                'menu_class'        => 'nav navbar-nav',
                'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                'walker'            => new wp_bootstrap_navwalker())
            );
        ?>
      </div><!-- /.container-fluid -->
    </nav>
</div><!-- #nav -->

As you may have noticed, basically you have to replace what is after your code's您可能已经注意到,基本上您必须替换代码之后的内容

 <!-- Collect the nav links, forms, and other content for toggling -->

with

<?php
    wp_nav_menu( array(
            'menu'              => 'primary',
            'depth'             => 2,
            'container'         => 'div',
            'container_class'   => 'navbar-collapse collapse',
            'menu_class'        => 'nav navbar-nav',
            'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
            'walker'            => new wp_bootstrap_navwalker())
        );
    ?>

and voila

You can replace the ul element inside the div of class collapse navbar-collapse with wp_nave_menu as follow example.您可以使用 wp_nave_menu 替换class collapse navbar-collapse的 div 中的 ul 元素,如下例所示。

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <?php wp_nav_menu(
                        array(
                            'theme_location' => 'primary',
                            'container' => false,
                            'menu_class' => 'nav navbar-nav navbar-right'))
                    ?>
                </div>

To fix the dropdown menu problem you should:要修复下拉菜单问题,您应该:

  1. go to class-wp-bootstrap-navwalker.php file转到class-wp-bootstrap-navwalker.php文件

  2. look for $atts['data-toggle'] ="dropdown";寻找$atts['data-toggle'] ="dropdown"; and replace it with $atts['data-bs-toggle'] ="dropdown";并将其替换为$atts['data-bs-toggle'] ="dropdown";

 <?php  wp_nav_menu( array('menu' => 'primary', ) ); ?>

Here is a my take to the issue that provided me the flexibility I needed to create a vertical, accordeon type, bootstrap 5 menu:这是我对这个问题的看法,它为我提供了创建垂直手风琴类型引导程序 5 菜单所需的灵活性:

https://github.com/exonianp/wp-bootstrap5-accordeon-vertical-menu https://github.com/exonianp/wp-bootstrap5-accordeon-vertical-menu

I opted to go the old fashioned way, and create a rather complex JOIN query because simply I had enough with the limitations of both the wp_get_nav_menu_items and the various walkers that are circulating around (that still use ,我选择了老式的方式,并创建了一个相当复杂的 JOIN 查询,因为我已经受够了wp_get_nav_menu_items和周围循环的各种步行者(仍然使用,

  • ) and need a lot of time to fully understand what they are doing. ) 并且需要大量时间来完全理解他们在做什么。

  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM