wp-bootstrap-navwalker icon indicating copy to clipboard operation
wp-bootstrap-navwalker copied to clipboard

dropdown/mobile not working - jquery issue?

Open bengiordano opened this issue 12 years ago • 11 comments

I can't get the dropdowns/mobile menu to work -- I think its a jquery enquing issue based on previous posts.

Functions file

<?php
/**
 * test functions and definitions
 *
 * @package wide
 */




// Register Custom Navigation Walker
require_once('wp_bootstrap_navwalker.php');


/**
 * add new navigation
 */
register_nav_menus( array(
        'primary' => __( 'Primary Menu', 'wide' ),
    ) );

header file

<?php
/**
 * The Header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="content">
 *
 * @package test
 */
?>

<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>

<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php wp_title( '|', true, 'right' ); ?></title>
<link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet" type="text/css" />
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">


<?php wp_head(); ?>

</head>

<body <?php body_class(); ?>>

<div id="page" class="">

    <?php do_action( 'before' ); ?>

<header>

<nav class="navbar navbar-default" 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>
      <a class="navbar-brand" href="<?php echo home_url(); ?>">
                <?php bloginfo('name'); ?>
            </a>
    </div>

        <?php
            wp_nav_menu( array(
                'menu'              => 'primary',
                '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())
            );
        ?>
    </div>
</nav>

</header>

    <div id="content" class="">

bengiordano avatar Mar 19 '14 19:03 bengiordano

I am using this and is working for me, just a quick question are you including the required collapse.js file to your project?

jvgraph avatar Mar 19 '14 19:03 jvgraph

Is that bundled in the bootstrap files? or do I have to include it manually?

bengiordano avatar Mar 19 '14 19:03 bengiordano

not sure how you have it setup but try adding that file here is a link where you can get all the js files. https://github.com/twbs/bootstrap

jvgraph avatar Mar 19 '14 19:03 jvgraph

It's bundled with the rest of the bootstrap files.

So you think this is related to a jQuery enqueue issue? What version of the jQuery library are you including and have you specified it as a dependency of the bootstrap javascript?

pattonwebz avatar Mar 19 '14 19:03 pattonwebz

Haha, two comments flew in as I was typing. Looks Like you guys have this one covered!

SideQuestDIY avatar Mar 19 '14 19:03 SideQuestDIY

Hey guys -- you are awesome, thanks so much for all the help.

I'm not sure I am including a version of jQuery manually and I know I haven't specified it as a dependency of the bootstrap js. Is there an example script you can point me to where this is done properly so I can configure in my theme in the same way?

bengiordano avatar Mar 19 '14 19:03 bengiordano

this is the function use to set up your scripts, but your html markup looks fine, one question did you register your menu?

add_action('wp_enqueue_scripts', 'register_myscripts_scripts');
function register_myscripts_scripts(){
wp_enqueue_script('jquery'); 
wp_register_script('bootstrap', get_stylesheet_directory_uri() . '/js/bootstrap.min.js', array(), false, true);
wp_enqueue_script( 'bootstrap' );
wp_register_script('collapse', get_stylesheet_directory_uri() . '/js/collapse.js', array(), false, true);
wp_enqueue_script( 'collapse' );
wp_register_script('scripts', get_stylesheet_directory_uri() . '/js/scripts.js', array(), false, true);
wp_enqueue_script( 'scripts' );
}

jvgraph avatar Mar 19 '14 19:03 jvgraph

@jvgraph Thank you for your help! There are too small issues in the code you shared.

First, you need to tell WordPress that the bootstrap.min.js file requires jQuery as the Bootstrap javascript components are jQuery plugins. It's as easy as adding the jquery string to the array after the file name.

wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '3.1.1', true );

Second, if you are including bootstrap.min.js it already contains the required Collapse code so loading collapse.js as well will cause javascript conflicts.

@bengiordano there is not need in load jQuery again as it is bundled with WordPress, but as mentioned above you must tell WordPress that Bootstrap requires jQuery.

SideQuestDIY avatar Mar 19 '14 20:03 SideQuestDIY

@jvgraph and @twittem

Got it! Here is the updated script in my functions file:

add_action('wp_enqueue_scripts', 'register_myscripts_scripts');
function register_myscripts_scripts(){
wp_enqueue_script('jquery'); 
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/inc/bootstrap/js/bootstrap.min.js', array( 'jquery' ), '3.1.1', true );
}

bengiordano avatar Mar 19 '14 21:03 bengiordano

@bengiordano awesome!

A quick note, you don't need to include the line wp_enqueue_script('jquery');

If you take a look at the Codex docs for wp_enqueue_script you will see the format for the function is:

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>

We define the $deps (or dependencies) variable as array( 'jquery' ) which tells WordPress to load jQuery into our theme. Because jQuery is already registered in WordPress's core there is no need to load it again.

SideQuestDIY avatar Mar 19 '14 22:03 SideQuestDIY

Amazing -- thank you so much for taking the time to help and then explain why. So excited to use this nav walker and bootstrap on my project.

On Wed, Mar 19, 2014 at 6:02 PM, Edward McIntyre [email protected]:

@bengiordano https://github.com/bengiordano awesome!

A quick note, you don't need to include the line wp_enqueue_script('jquery');

If you take a look at the Codex docs for wp_enqueue_script you will see the format for the function is:

We define the $deps (or dependencies) variable as array( 'jquery' ) which tells WordPress to load jQuery into our theme. Because jQuery is already registered in WordPress's core there is no need to load it again.

Reply to this email directly or view it on GitHubhttps://github.com/twittem/wp-bootstrap-navwalker/issues/87#issuecomment-38112820 .

bengiordano avatar Mar 19 '14 22:03 bengiordano