简体   繁体   中英

JQuery not loading wordpress 4

Jquery doesn't work on local install of WordPress. I have loaded the necessary files in functions.php as follows:

   <?php 

     function abdi_theme_styles(){

      wp_enqueue_style( 'normalize_css', get_template_directory_uri () .   '/css/normalize.css');
      wp_enqueue_style( 'Google-font', 'http://fonts.googleapis.com/css?family=Changa+One|Open+Sans:400italic,700italic,400,700,800');
      wp_enqueue_style( 'main_css', get_template_directory_uri () . '/style.css');
      wp_enqueue_style( 'responsive.css', get_template_directory_uri () . '/css/responsive.css');
}

     add_action( 'wp_enqueue_scripts', 'abdi_theme_styles' ); 

    function abdi_theme_js(){
      wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/main.js', array('jquery'), '', true );
}

    add_action( 'wp_enqueue_scripts', 'abdi_theme_js' ); 


    ?>

I checked the resources that load into the browser, but my app.js file returns a 404. The file does exits and is actually inside the js folder. I have also wrapped my jquery code using the no conflict wrapper - here is my main.js file:

jQuery(document).ready(function($) {

        $(document).ready(function(){
          alert('Ready!');
          var lastScrollTop = 0;
          $(window).scroll(function(event){
           var st = $(this).scrollTop();
            if (st > lastScrollTop){
            $('header').slideUp(500); 
             } else {
              $('header').slideDown(500);  
       }
      lastScrollTop = st;
    });

     });//end read function 
    });//end noConflict wrapper

I'm using XAMPP on a Linux Ubuntu platform and I'm using Wordpress version 4.2.2 Any suggestions?

In case anyone is having a similar issue, I have found what the problem was.

I got it working by removing the version and in footer arguments to the wp_enqueue_script function:

wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/main.js', array('jquery')); 

These arguments are supposed to specify which version of the dependent file that you want to use and whether you want to load it in the head or near the footer, but that was what was causing jquery not work for me.

Would be interested to see if anyone else is having the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM