为什么我使用自定义主题打开WordPress页面时会出现此JQuery错误?

I am pretty new as WordPress developer and I have the following problem.

I have create the template of this website starting from 0 by myself: http://www.asper-eritrea.com

Now I have add this photo gallery plugin: https://wordpress.org/plugins/robo-gallery/

The problem is that now, when I access to a page that use this plugin, for example: http://www.asper-eritrea.com/photo-gallery/

the gallery is not shown and, into the FireBug console, I obtain this error message:

TypeError: $ is not a function
    $(document).ready(function() {

So I think that maybe it could depende by the fact that it can't retrieve JQuery, but I am absolutly not sure about it because I have the same error also opening the others page of my website so I am not sure that the missing of JQuery into the photogallery page is the cause of my problem.

Into the function.php file of my template I have the following function that enqueue all the JavaScript resources (including JQuery), in this way:

/* Hooks a function on to a specific action (an action is a PHP function that is executed at specific
 * points throughout the WordPress Core)
 * @param 'wp_enqueue_scripts': The name of the action to which 'wpb_adding_styles' is hooked (wp_enqueue_scripts
 *        is the proper hook to use when enqueuing items that are meant to appear on the front end)
 */
add_action('wp_enqueue_scripts', 'wpb_adding_styles');

/* Function automatically executed by the hook 'load_java_scripts':
 * 1) Load all my JavaScripts
 */
function load_java_scripts() {

    // Load JQuery:
    wp_enqueue_script('jquery');
    // Load FlexSlider JavaScript
    wp_enqueue_script('flexSlider-js', get_template_directory_uri() . '/assets/plugins/flexslider/jquery.flexslider.js', array('jquery'), 'v2.1', true);
    // Load bootstrap.min.js:
    wp_enqueue_script('bootstrap.min-js', get_template_directory_uri() . '/assets/bootstrap/js/bootstrap.min.js', array('jquery'), 'v3.0.3', true);

    // Load FancyBox:
    wp_enqueue_script('fancy-js', get_template_directory_uri() . '/assets/plugins/fancybox/jquery.fancybox.pack.js', array('jquery'), 'v2.1.5', true);
    // Load scripts.js:
    wp_enqueue_script('myScripts-js', get_template_directory_uri() . '/assets/js/scripts.js', array('jquery'), '1.0', true);
    // Load Modernizer:
    wp_enqueue_script('myodernizer-js', get_template_directory_uri() . '/assets/js/modernizr.custom.js', array('jquery'), '2.6.2', true);

}

add_action('wp_enqueue_scripts', 'load_java_scripts');

So what is wrong? What am I missing?

The strange thing is that to enqueue JQuery there is:

wp_enqueue_script('jquery');

but there is not specified the exact folder of the Jquery source file (as done for the other resources).

I have JQuery putted into the /assets/bootstrap/js/jquery.js file.

1个回答



Wordpress通常包括 jQuery.noConflict()删除 $ </ code>别名以防止与可能也在使用 $ </ code> </ p>的其他库发生冲突

< p>更改:</ p>

  $(document).ready(function(){
</ code> </ pre>

To </ p >

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

//可以使用$here
</ code> </ pre>
</ DIV>

展开原文

原文

Wordpress generally includes jQuery.noConflict() which removes the $ alias to prevent collisions with other libraries that may also be using $

Change:

 $(document).ready(function() {

To

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

     // OK to use `$` here

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问