如何将按钮元素用于tgetWordPress菜单项?

时间:2018-08-20 作者:Robert Andrews

如何混合两个菜单导航栏的HTML元素类型?

我正在用Bootstrap 4构建WordPress主题。

WordPress的菜单内容应来源于菜单选项,输出到Bootstrap 4导航栏。我正在使用this Bootstrap 4-specific wp_nav_menu navwalker ...

require_once get_template_directory() . \'/inc/bs4navwalker.php\';
。。。要将导航文本链接的主菜单输出为无序列表,请执行以下操作God intended...

 <!-- Navbar https://getbootstrap.com/docs/4.1/components/navbar/ -->
 <nav class="navbar navbar-expand-md navbar-light"><!-- collapse at stated breakpoint -->
     <a class="navbar-brand" href="<?php echo esc_url( home_url( \'/\' ) ); ?>">
       <img src="https://www.example.com/logo.gif" height="30" class="d-inline-block align-top mr-1">
       <?php bloginfo( \'name\' ); ?>
     </a>
     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs4navbar" aria-controls="bs4navbar" aria-expanded="false" aria-label="Toggle navigation">
       <span class="navbar-toggler-icon"></span>
     </button>
     <?php
     wp_nav_menu([ // bs4navwalker menu generator
       \'menu\'            => \'primary\', // name of wordpress menu
       \'theme_location\'  => \'primary\',
       \'depth\'           => 2,
       // <nav>:
       // \'container\'       => \'false\',
       \'container_id\'    => \'bs4navbar\',
       \'container_class\' => \'collapse navbar-collapse\',
       // <ul>:
       \'menu_id\'         => false,
       \'menu_class\'      => \'navbar-nav ml-auto\',
       // other:
       \'fallback_cb\'     => \'bs4navwalker::fallback\',
       \'walker\'          => new bs4navwalker()
     ]);
     ?>
     <button class="btn btn-outline-primary mx-3" type="submit">Button</button>
     <button class="btn btn-primary ml-3" type="submit">Sign up</button>
 </nav>
这很好用。输出为。。。

enter image description here

<nav class="navbar navbar-expand-md navbar-light"><!-- collapse at stated breakpoint -->
     <a class="navbar-brand" href="http://www.example.com/">
       <img src="https://www.example.com/logo.gif" height="30" class="d-inline-block align-top mr-1">
              Sandbox     </a>
     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs4navbar" aria-controls="bs4navbar" aria-expanded="false" aria-label="Toggle navigation">
       <span class="navbar-toggler-icon"></span>
     </button>
     <div id="bs4navbar" class="collapse navbar-collapse">
       <ul id="menu-my-menu" class="navbar-nav ml-auto">
         <li id="menu-item-3" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-3 nav-item mx-3 active"><a title="Home Page" href="http://sandbox.contexthq.com/" class="nav-link active" aria-expanded="false"><i class="icon-speedometer"></i>Home</a></li>
         <li id="menu-item-4" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-4 nav-item mx-3"><a title="Example Page" href="http://example.com/" class="nav-link" aria-expanded="false"><i class="ti-layout-grid2"></i>Trends</a></li>
         <li id="menu-item-5" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-5 nav-item mx-3"><a title="Custom Page" href="http://example.com/custom/" class="nav-link" aria-expanded="false"><i class="ti-palette"></i>Custom</a></li>
         <li id="menu-item-14" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-14 nav-item mx-3"><a href="#" class="nav-link" aria-expanded="false">Strategy</a></li>
       </ul>
     </div>
     <button class="btn btn-outline-primary mx-3" type="submit">Button</button>
     <button class="btn btn-primary ml-3" type="submit">Sign up</button>
 </nav>
但是,您将看到我手动插入两个<button> elements. 与硬编码不同,我希望这些代码是从WordPress菜单动态生成的,就像primary 菜单

我制作了第二个WordPress菜单,buttons. 这就是我崩溃的地方。。。

我可以插入第二个wp_nav_menu 需要buttons, 就在第一个之后primary...

 <?php
 wp_nav_menu([ // bs4navwalker menu generator
   \'menu\'            => \'buttons\', // name of wordpress menu
   \'theme_location\'  => \'buttons\',
   \'depth\'           => 2,
   // <nav>:
   // \'container\'       => \'false\',
   \'container_id\'    => \'bs4navbar\',
   \'container_class\' => \'collapse navbar-collapse\',
   // <ul>:
   \'menu_id\'         => false,
   \'menu_class\'      => \'navbar-nav ml-auto\',
   // other:
   \'fallback_cb\'     => \'bs4navwalker::fallback\',
   \'walker\'          => new bs4navwalker()
 ]);
 ?>
。。。菜单项将插入到正确的位置。

问题是,这些项目<li>s在a内<ul>, 就像primary 菜单

enter image description here

我想bs4navwalker 设置为输出所有wp_nav_menu 项目作为列表项目,或者WordPress正在这样做。

我怎样才能确保第二个菜单中的项目,buttons, 使用不同的HTML结构获取输出,如<button> 而是元素?

1 个回复
SO网友:Robert Andrews

下面是一个解决方案,虽然使用了额外的PHP处理。。。

 <?php

 $options =  array(
     \'echo\'              => false,
     \'theme_location\'    => \'buttons\',
     \'items_wrap\'        => \'%3$s\',
     \'menu_class\'        => false,
     \'menu_id\'           => false,
     \'container\'         => \'false\',
     \'container_class\'   => false,
     \'container_id\'      => false,
     \'before\'            => \'<button class="btn btn-primary mx-3 d-none d-md-block" type="submit">\',
     \'after\'             => \'</button>\'
 );

 $menu = wp_nav_menu($options);

 $output = strip_tags($menu, \'<button>\');

 echo preg_replace(\'/btn-primary/\', \'btn-outline-primary\', $output, 1);

 ?>
这包括:

移除<ul><button> 标签

  • 抑制回声
  • 剥离所有标签(即。<li>) 除了<button>)btn-primary 使用类btn-outline-primary, 以保留两种颜色的按钮

    enter image description here

    <button class="btn btn-outline-primary mx-3 d-none d-md-block" type="submit">Button</button>
    <button class="btn btn-primary mx-3 d-none d-md-block" type="submit">Learn More</button>
    

    I would have preferred to set the button colour classes using WordPress\' own functionality for setting Menu item classes, rather than hard-code them here.

    Is there a way to get those classes appearing in the nav item, instead of using preg_replace, or would this method not support it?

  • 结束

    相关推荐

    Custom Menus and taxonomies

    好的,我刚开始使用WordPress,不是很好,我有一个菜单问题,我真的需要一些帮助。我在一个网站上工作,它有多个地点,并非所有地点都销售相同的产品。所有位置都是具有父结构的页面。俄亥俄州-联系我们-关于我们-产品等。。。西弗吉尼亚州-联系我们-关于我们-产品等。。。宾夕法尼亚州-联系我们-关于我们-产品等我掌握了自定义页面模板的窍门,并为其添加了自定义菜单(因此,我所有的Ohio页面都使用了我制作的自定义Ohio模板,并包含了自定义Ohio菜单)。我正在使用帖子创建产品信息页面,并使用以下方式显示它们: