Having Trouble with the WordPress Navbar Collapsed Button in Bootstrap

I've been attempting to integrate a bootstrap navbar into a customized theme, but I'm struggling to understand why it's not functioning as expected.

This snippet is extracted from the header.php source code.

(Please note: the navbar works perfectly, except for the collapsed navbar button when viewed on mobile devices)

Thank you in advance

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title><?php echo get_the_title( $ID ); ?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="<?php bloginfo('stylesheet_url');?>" rel="stylesheet">
  <?php wp_enqueue_script("jquery"); ?>
  <?php wp_head(); ?>
</head>
<body>

<div id="header">
  <div class="navbar navbar-fixed-top" role="navigation">
      <div class="container">
        <button class="btn btn-navbar" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand"><img src="<?php bloginfo('template_directory'); ?>/img/logo.png" 
         alt="Electricians in Cardiff" class="img-responsive"></a>
        <div class="collapse navbar-collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="/index.php">Home</a></li>
            <li><a href="/about/">About</a></li>
            <li><a href="/services/">Services</a></li>
            <li><a href="/blog/">Blog</a></li>
            <li><a href="/areas-we-cover/">Areas</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
  </div>
</div>

<div class="container">

I followed a tutorial and added the following to my style sheet:

@import url('bootstrap/css/bootstrap.css'); 
@import url('bootstrap/css/bootstrap-responsive.css'); 
body { 
    padding-top: 70px; 
}

The below code is included in functions.php file:

<?php 

function wpbootstrap_scripts_with_jquery()
{
    // Register the script like this for a theme:
    wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/js/bootstrap.js', array( 'jquery' ) );
    // For either a plugin or a theme, you can then enqueue the script:
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );


if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
?>

Answer №1

Code needs some corrections:

<button class="btn btn-navbar" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">>

Should be:

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">

Take note of the duplicate "class" attributes: class="btn btn-navbar" and class="navbar-toggle"

In addition, there are some misplaced DIVs.

Improved version of the code:

<div id="header">
    <div class="navbar navbar-fixed-top" role="navigation">
        <div class="container">

           <div class="navbar-header">
               <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                 <span class="icon-bar"></span>
                 <span class="icon-bar"></span>
                 <span class="icon-bar"></span>
               </button>
               <a class="navbar-brand"><img src="/img/logo.png" 
alt="Electricians in Cardiff" class="<?php bloginfo('template_directory'); ?>/img-responsive"></a>
           </div>

           <div class="collapse navbar-collapse" id="myNavbar">
              <ul class="nav navbar-nav navbar-right">
                 <li><a href="/index.php">Home</a></li>
                 <li><a href="/about/">About</a></li>
                 <li><a href="/services/">Services</a></li>
                 <li><a href="/blog/">Blog</a></li>
                 <li><a href="/areas-we-cover/">Areas</a></li>
               </ul>
           </div><!--/.nav-collapse -->
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Excessive image display | HTML and CSS synergize

I am having some trouble with my image gallery. Each image is displayed as a vertical column and has zooming effects when hovered over. Clicking on an image should show it in full screen with a caption. The problem arises when dealing with images that are ...

Displaying jQuery UI datepicker when an asp:LinkButton is clicked

How can we display the Jquery UI datepicker when clicking an asp.net linkbutton? This is the code snippet: <asp:LinkButton ID="PublishedSortLinkButton" runat="server" cssclass="datepicker" Text="<%$ Resources:Vacancies, DateRang ...

Exploring the world of Django static files, along with the magic of CSS and

Currently running on django 1.3 for production and encountering an issue with using static files in my CSS and JavaScript. Strangely, there are no problems in my HTML code! How can I resolve this dilemma? Unfortunately, the Django documentation does not pr ...

Tips for inserting text to the left rather than the right using Jquery

I recently came across this code snippet shared by some helpful users on stackoverflow and it has been working perfectly for me. However, I do have a couple of queries regarding its functionality. Firstly, how can I ensure that the current selected option ...

Changing the text of a button using ngClass in Angular's toggle functionality

Is there a way to modify the text of a toggled button class in Angular using Bootstrap? Currently, I have this code to toggle the class: html <!-- toggle button --> <button type="button" class="btn btn-primary mt-3 ml-3" (click)="status=!status ...

It appears that the jQuery this() function is not functioning as expected

Despite all aspects of my simple form and validation working flawlessly, I am facing an issue where 'this' is referencing something unknown to me: $('#contact').validator().submit(function(e){ e.preventDefault(); $.ajax ...

What methods can be used to keep track of Ajax requests?

Within my HTML page, I have implemented multiple JQuery Ajax calls using methods like get and post. I am interested in monitoring the states of these ajax calls without relying on the "success" or "error" methods. Is there an alternative method for trackin ...

What is the best way to send back data as a response after making a $.post request in PHP?

Looking to set up a basic post function using jQuery that takes the client's name as input and returns their information (address, phone, age, etc.). Here's what I have so far: Script: $(document).ready(function(){ $("#getClientInfo").on(&a ...

The close button on bootstrapselect is oversized

Having an issue with implementing the bootstrap select search and close button in my view. An image has been uploaded through the following link: View the bootstrap select dropdown image here Javascript $('#class_list_for_fee_report').multisel ...

Splitting areas within a liquid vessel using bootstrap 5

My container is structured like this with divisions: <div class="container-fluid"> <div class="row"> <div class="col-3"> sidebar </div> <div class="col-7"> top slider </div> <di ...

Using JQuery to attach an onclick event to multiple links that share the same class

There are links on the website with the ".tel" class <a class='tel' href="http://example.com">Link</a> To add an onclick event to all these links, follow these steps onclick="return gtag_report_conversion('http:// ...

Issue with Jquery event not triggering correctly following ajax data retrieval

This script uses jQuery and Ajax to populate a navigation bar with categories and subcategories dynamically. The first unordered list is static, while the second one is populated after receiving data via Ajax. There are some jQuery events that need to be i ...

What is the solution to eliminating the horizontal scroll bar on a responsive website?

Currently in the process of developing a responsive website, I have encountered an issue with excess empty space appearing on the right side when scrolling horizontally. Utilizing overflow-x: hidden successfully eliminates the horizontal scroll; however, t ...

Delete the radio button list within the content placeholder

I'm having trouble with accessing elements by name on a master page content placeholder. Can someone assist me with this issue? Thank you in advance. <asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <script type="tex ...

JQuery Mobile Drag and Drop Feature for Easy Reordering of Items in Drop Zones

Seeking assistance in finding a suitable mobile web solution for managing the user interface when selecting horses in a race to "win," "place," or "show." I envision users seeing a short list of horse names with small pictures, which they can then drag in ...

What is the best way to position a drop-down box in front of images?

After creating a gallery page to showcase website images, I encountered an issue where the images overlapped with the drop-down boxes of my navigation bar. Unfortunately, this means that I am unable to click on the drop-down boxes in order to navigate to o ...

When using IE10/IE11 in compatibility mode 8, there is an issue with the jquery.fileupload.js (v9.5.2) where uploading a

We are facing a challenge where we need to use IE10/IE11 in compatibility mode IE 8 to support older online sites that utilize iframes and newer html5 applications built with technologies like jquery and angular. Specifically, we are encountering an issue ...

How does using position: fixed affect width in React components?

Can someone explain why my sidebar width is reduced when I apply position: fixed? Here is the code snippet: https://codesandbox.io/s/1yr3nlqp74 Steps to reproduce the bug: Open the code in a new window (full screen) Observe the picture before and after ...

Attempting to align two text boxes in the center using the text-align property

I've been attempting to center two text boxes using text-align. Unfortunately, they didn't align properly, as evident in this snapshot: https://gyazo.com/c47f4037ba1ab3abef5833358d94172e. What could have caused this misalignment, and how can it b ...

What are some ways to create responsive elements using Bootstrap?

I'm looking for assistance in using bootstrap to divide a screen into 8 responsive parts (2 at the top, 2 in the middle, and 2 at the bottom). Can anyone help me with this? ...