“未定义不是函数"当我添加WordPress插件时 [英] "undefined is not a function" when I add a WordPress plugin

查看:67
本文介绍了“未定义不是函数"当我添加WordPress插件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用自己的jQuery和两个插件(jCarousel,Cycle2)创建一个网站,另外两个来自WordPress(高级自定义字段和CPT UI).它一直运行顺利,但是现在当我尝试添加新的WordPress插件时(我已经尝试了Contact Form 7和Kento Ajax Contact Form),我收到了"undefined is not a function"错误,标记在最后一行以下代码片段:

I'm creating a website with my own jQuery and two plugins (jCarousel, Cycle2), and other two from WordPress (Advanced Custom Fields and CPT UI). It has been running smoothly, but now when I try to add a new WordPress plugin (I've tried both Contact Form 7 and Kento Ajax Contact Form), I get the "undefined is not a function" error, marked at the last line of the following piece of code:

// Set jCarousel configuration
        $(function() {
            var hash = 0;

            if (window.location.hash) {
                hash = parseInt(window.location.hash.slice(1));
                hash--;
            }

            $('.jcarousel').on('jcarousel:createend', function() {
                $(this).jcarousel('scroll', hash, false);
            }).jcarousel(); 

(在那段代码之后,我有更多的jCarousel配置,但是我用}结尾了);完成后).

(After that piece of code I have a lot of more jCarousel configuration, but I end it properly with }); when I'm done).

我在$(function(){/*代码*/})中封装了几段jQuery; ,一个在jCarousel配置之前,一个在7之后,这有什么问题吗?

I've got several pieces of jQuery encapsulated within $(function() { /* code here */ }); , one before the jCarousel configuration and 7 after, is there any problem with that?

我已经尝试将$更改为jQuery以及以下代码:

I've already tried to change $ to jQuery and also the following code:

e(function ($) { $(document); }(jQuery));

我还能尝试解决什么问题?

What else can I try to solve this problem?

推荐答案

我发现了问题.我正在像这样在我的header.php文件的上加载jQuery:

I found the problem. I was loading jQuery on the of my header.php file like this:

<script src="<?php bloginfo('template_url'); ?>/scripts/jquery.min.js" type="text/javascript"></script>

这导致jQuery被加载两次,因为插件正在排队.我必须正确包含jQuery和类似的插件:

This caused jQuery to be loaded twice, because the plugins were enqueuing it. I had to properly include jQuery and the plugins like this:

function load_plugins() {
if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, '1.11.1', true);
    wp_enqueue_script('jquery');

    wp_enqueue_script('cycle2', 'http://malsup.github.com/jquery.cycle2.js', array('jquery'));
    wp_enqueue_script('jcarousel', get_template_directory_uri() . '/js/jquery.jcarousel.min.js', array('jquery'));
} } add_action('init', 'load_plugins');

然后将我的jQuery代码封装在页脚上,如下所示:

And then encapsulate my jQuery code on the footer like this:

(function($) {
    /* jQuery code here */
}(jQuery));

这篇关于“未定义不是函数"当我添加WordPress插件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆