You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress Bootstrap Navwalker移动端展开问题求助

Fixing WordPress Bootstrap Navwalker Mobile Collapse Issue

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

  1. Nested <ul> Tags: You wrapped the wp_nav_menu() call inside a manual <ul class="nav navbar-nav">, but the navwalker already generates this <ul> for you (via the menu_class parameter). This creates invalid nested list structure that breaks Bootstrap's collapse functionality.
  2. Incorrect Container Placement: The container parameter in wp_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 the navbar-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.php is in your theme's root directory (the same folder as functions.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:03:44