php 将 Bootstrap 导航栏转换为 WordPress 菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25332527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Bootstrap navbar to WordPress menu
提问by user3574492
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 网站和其他教程的链接,我将不胜感激,因为我已经尝试了很多东西
回答by Devin
You need to use this https://github.com/twittem/wp-bootstrap-navwalker, add the nav walker file and follow the instructions. 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. It's incredible easy, check it out:
你需要使用这个https://github.com/twittem/wp-bootstrap-navwalker,添加 nav walker 文件并按照说明进行操作。这是我制作的一个随机站点的示例,我不会将其调整到您自己的站点,因为您需要在未来的所有 WP 开发中学习这一点。这非常简单,请查看:
<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
瞧
回答by Khachornchit Songsaen
You can replace the ul element inside the div of class collapse navbar-collapsewith 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>