在WordPress中添加Java脚本时出现问题

时间:2020-04-05 作者:Bex

我正在将boostrap主题传输到WordPress,并尝试在中添加JavaScript文件functions.php 执行时没有显示错误:

<?php
function regload_javascripts() //need to register first
{
   wp_register_script(\'bootstrapjs\',  get_template_directory_uri().\'/js/bootstrap.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'bootstrapjs\');
   wp_register_script(\'carousel\',  get_template_directory_uri().\'/js/carousel.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'carousel\');
   wp_register_script(\'custom\',  get_template_directory_uri().\'/js/custom.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'custom\');
   wp_register_script(\'flexslider\',  get_template_directory_uri().\'/js/flexslider.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'flexslider\');
   wp_register_script(\'jquery182\',  get_template_directory_uri().\'/js/jquery-1.8.2.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'jquery182\');
   wp_register_script(\'cslider\',  get_template_directory_uri().\'/js/jquery.cslider.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'cslider\');
   wp_register_script(\'gmap_min\', get_template_directory_uri().\'/js/jquery.gmap.min.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'gmap_min\');
   wp_register_script(\'slider\',  get_template_directory_uri().\'/js/slider.js\',array(\'jquery\'),\'1.1\', true);
   wp_enqueue_script(\'slider\');
}

add_action (\'wp_enqueue_scripts\', \'regload_javascripts\');
但没有效果。

js文件如下:js files

我的开发人员控制台输出为:

TypeError: $ is undefined carousel.js:3:6079    <anonymous> http://localhost/wordpress/wp-content/themes/bootgotya/js/carousel.js?ver=1:3
TypeError: $ is not a function flexslider.js:14:2     <anonymous> http://localhost/wordpress/wp-content/themes/bootgotya/js/flexslider.js?ver=1:14 
Loading failed for the <script> with source “http://localhost/wordpress/js/flexslider.js”. wordpress:590:1 
Loading failed for the <script> with source “http://localhost/wordpress/js/carousel.js”. wordpress:591:1 
Loading failed for the <script> with source “http://localhost/wordpress/js/jquery.cslider.js”. wordpress:592:1

1 个回复
SO网友:Bex

正如上面的评论和建议中提到的,删除jQuery 1.8.2文件的脚本解决了我的问题。谢谢大家:)!

相关推荐

如何在wp_post`post_content`中剥离javascript

我的网站正在遭受SQL注入攻击,重定向到广告网站dolohen[dot]com。我需要在提供javascript之前去掉它。Is there a filter I can add or update to remove everything between tags in wp_posts post_content我不熟悉wordpress编程,但不熟悉sql和数据库。这有点让人困惑,因为有些人想在帖子中加入javascript,但却不能。然而,这些SQL注入器似乎是随心所欲的!ThanksPhil公