[英]Wordpress plugin doesn't work
Spin360 doesn't work: All scripts are connected into functions.php Spin360不起作用:所有脚本都连接到functions.php中
add_action('wp_footer', 'add_scripts');
function add_scripts() {
if(is_admin()) return false;
wp_deregister_script('jquery');
wp_enqueue_script('remodal', 'http://code.jquery.com/jquery-3.0.0.min.js','', '',true);
wp_enqueue_script('remodal', get_template_directory_uri().'/js/jquery.threesixty.js','','',true);
wp_enqueue_script('remodal', get_template_directory_uri().'/js/spinspin.js','','',true);
}
Jquery.threesixty.js -> http://www.mathieusavard.info/threesixty/demo.html Jquery.threesixty.js-> http://www.mathieusavard.info/threesixty/demo.html
Spinspin.js: Spinspin.js:
jQuery(function($){
$("#spin").threesixty({images:["...", "..."], method:'click', direction:'forward', sensibility: 1});
});
Article page: 文章页面:
<div id="spin"></div>
<script> if (window.jQuery) {
alert('Connected'); //Return Connected
}
</script>
Page -> http://okwood.by/2016/05/iv78-optima/ 页面-> http://okwood.by/2016/05/iv78-optima/
Since wordpress uses jQuery.noConflict() to prevent collisions with any other library that might use $
alias you either need to replace $
in your code with jQuery
or insulate your code in an IIFE like: 由于wordpress使用jQuery.noConflict()来防止与可能使用
$
别名的任何其他库发生冲突,因此您需要用jQuery
替换代码中的$
或将代码隔离在IIFE中:
;(function($){
// make sure document is ready
$(function(){
$("#spin").threesixty({images
});
})(jQuery);
OR 要么
jQuery(function($){
$("#spin").threesixty({images
});
WordPress comes pre-packaged with a copy of jQuery, which you should use with your code. WordPress预包装了jQuery副本,您应该在代码中使用它。 When WordPress' jQuery is loaded, it uses compatibility mode, which is a mechanism for avoiding conflicts with other language libraries.
加载WordPress的jQuery时,它使用兼容模式,这是一种避免与其他语言库冲突的机制。
You can't use $
in jQuery script, try instead jQuery
(replace $
with jQuery
). 您不能在jQuery脚本中使用
$
,而是尝试使用jQuery
(用jQuery
替换$
)。
Wrap your code like this 这样包装代码
jQuery(document).ready(function( $ ) {
//Code Here
});
Wordpress ships with jQuery in noConflict mode but when you load jQuery from CDN I don't think that the case. WordPress在noConflict模式下附带了jQuery,但是当您从CDN加载jQuery时,我认为情况并非如此。 Another point is that the first param for wp_enqueue_script() function is
string $handle
, each handle should be unique. 另一点是wp_enqueue_script()函数的第一个参数是
string $handle
,每个句柄都应该是唯一的。 It is also highly recommended to define script dependencies to ensure loading of all the dependencies it needs. 强烈建议定义脚本依赖项,以确保加载所需的所有依赖项。 Try this example:
试试这个例子:
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://code.jquery.com/jquery-3.0.0.min.js', [], false, true );
wp_register_script( 'threesixty', get_template_directory_uri().'/js/jquery.threesixty.js',[ 'jquery' ], false, true );
wp_enqueue_script( 'remodal', get_template_directory_uri().'/js/spinspin.js', [ 'threesixty' ], false, true );
And very important, wp_enqueue_scripts is the proper hook to use when enqueuing items, not wp_footer
. 而且非常重要的是, wp_enqueue_scripts是在将项目
wp_footer
时使用的适当钩子,而不是wp_footer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.