WordPress Bootstrap Navwalker移动端展开问题求助
Hey there! I can see why your mobile menu isn't expanding—let's break down the issues in your code and fix them step by step.
The Core Problems
- Nested
<ul>Tags: You wrapped thewp_nav_menu()call inside a manual<ul class="nav navbar-nav">, but the navwalker already generates this<ul>for you (via themenu_classparameter). This creates invalid nested list structure that breaks Bootstrap's collapse functionality. - Incorrect Container Placement: The
containerparameter inwp_nav_menu()is set to create the collapse div, but you placed it inside a manual<ul>instead of having it as a sibling to thenavbar-header—which is required for Bootstrap's toggle to work properly.
Corrected Navigation Code
Here's the fixed version of your nav code, with comments explaining the changes:
<nav class="main-menu navbar navbar-bg thetop" 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> <!-- navbar-brand is hidden on larger screens, but visible when the menu is collapsed --> <a class="navbar-brand" href="<?php echo home_url(); ?>"> <?php if (function_exists('the_custom_logo')) { the_custom_logo(); } else{ bloginfo('name'); } ?> </a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <?php wp_nav_menu( array( 'theme_location' => 'primary', 'depth' => 2, 'container' => 'div', 'container_class' => 'collapse navbar-collapse', 'container_id' => 'bs-example-navbar-collapse-1', 'menu_class' => 'nav navbar-nav', 'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback', 'walker' => new WP_Bootstrap_Navwalker() ) ); ?> <!-- Search Form --> <form id="searchform" class="navbar-form navbar-right" role="form" action="<?php echo home_url( '/' ); ?>"> <div class="input-group"> <input type="text" class="form-control" name="s" placeholder="Ara"> <span class="input-group-btn"> <button type="submit" class="btn btn-default btn-primary -red"> <span class="glyphicon glyphicon-search"></span> </button> </span> </div> <a href="#" class="btn btn-top-cta affirmation hidden-xs hidden-sm"></a> </form> </div> </nav>
Additional Checks to Ensure It Works
- Confirm Bootstrap Assets Are Loaded: Make sure your theme is properly including Bootstrap's CSS and JavaScript (Bootstrap depends on jQuery, so jQuery must load before Bootstrap JS).
- Verify Navwalker File: Double-check that
wp-bootstrap-navwalker.phpis in your theme's root directory (the same folder asfunctions.php), and that the file is up-to-date with the latest version of the navwalker.
That should get your mobile menu expanding correctly! Let me know if you run into any other issues.
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

