项目作者: AlexWebLab

项目描述 :
Bootstrap 5 WordPress navbar walker menu.
高级语言: PHP
项目地址: git://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker.git
创建时间: 2020-12-18T06:45:00Z
项目社区:https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker

开源协议:MIT License

下载


Bootstrap 5 WordPress navbar walker menu

bootstrap-5-wordpress-navbar-walker

How to use:

  1. Copy and paste the bootstrap_5_wp_nav_menu_walker class into the functions.php file of your theme;
  2. Register a new menu by adding the follow code into the functions.php file of your theme:
    1. register_nav_menu('main-menu', 'Main menu');
  3. Add the following html code in your header.php file or wherever you want to place your menu:

    1. <nav class="navbar navbar-expand-md navbar-light bg-light">
    2. <div class="container-fluid">
    3. <a class="navbar-brand" href="#">Navbar</a>
    4. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main-menu" aria-controls="main-menu" aria-expanded="false" aria-label="Toggle navigation">
    5. <span class="navbar-toggler-icon"></span>
    6. </button>
    7. <div class="collapse navbar-collapse" id="main-menu">
    8. <?php
    9. wp_nav_menu(array(
    10. 'theme_location' => 'main-menu',
    11. 'container' => false,
    12. 'menu_class' => '',
    13. 'fallback_cb' => '__return_false',
    14. 'items_wrap' => '<ul id="%1$s" class="navbar-nav me-auto mb-2 mb-md-0 %2$s">%3$s</ul>',
    15. 'depth' => 2,
    16. 'walker' => new bootstrap_5_wp_nav_menu_walker()
    17. ));
    18. ?>
    19. </div>
    20. </div>
    21. </nav>

    v1.3.0 Added support for dropdown menu (responsive) alignment: https://getbootstrap.com/docs/5.0/components/dropdowns/#menu-alignment

Here is how it works:

  • From Appearance -> Menus page of WordPress, check the CSS Classes checkbox under Screen Options;
  • Add a Custom Link with “#” in the URL field (this would be the parent of your dropdown);
  • On the CSS Classes field add any of the following alignment classes: ‘dropdown-menu-start’, ‘dropdown-menu-end’, ‘dropdown-menu-sm-start’, ‘dropdown-menu-sm-end’, ‘dropdown-menu-md-start’, ‘dropdown-menu-md-end’, ‘dropdown-menu-lg-start’, ‘dropdown-menu-lg-end’, ‘dropdown-menu-xl-start’, ‘dropdown-menu-xl-end’, ‘dropdown-menu-xxl-start’, ‘dropdown-menu-xxl-end’;
  • If any of the mentioned above class is detected, then they will automatically copied into the ul.dropdown-menu element following the Bootstrap 5 structure;
  • Any other class that is not related to the dropdown menu alignment will stay where it is.