最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Problem with JavaScripts addition in WordPress

programmeradmin2浏览0评论

I am transferring boostrap theme to WordPress and trying to add JavaScript files in functions.php and execution shows no error:

<?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');

But there is no effect.

The js files are here: js files

My dev console output is :

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

I am transferring boostrap theme to WordPress and trying to add JavaScript files in functions.php and execution shows no error:

<?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');

But there is no effect.

The js files are here: js files

My dev console output is :

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
Share Improve this question edited Apr 5, 2020 at 20:26 Bex asked Apr 5, 2020 at 18:53 BexBex 112 bronze badges 5
  • Are you sure the files are in the correct directory? Are you sure jquery is loaded? A screenshot doesn't say anything. The scripts could be loaded but used incorrectly elsewhere in the code. – cjbj Commented Apr 5, 2020 at 19:00
  • 1 What does your dev console say? Are there errors? Without knowing what the javascript is it's not possible to answer this question with just the information provided, please update your question via the edit link to include as much info as possible. Also, you shouldn't enqueue the jQuery 1.8.2 file, WP already has jQuery, and it might clash with your own version – Tom J Nowell Commented Apr 5, 2020 at 19:16
  • Yes, the directory if ok. Jquery is loaded, because previously there was a problem with insufficient memory: solved it with var $ = jQuery.noConflict(); – Bex Commented Apr 5, 2020 at 19:27
  • updated wit js files attached. – Bex Commented Apr 5, 2020 at 19:33
  • After removing enqueue the jQuery 1.8.2 file. The issue was solved!!! :) Thank you very much guys for instant help!!! – Bex Commented Apr 5, 2020 at 19:38
Add a comment  | 

1 Answer 1

Reset to default 0

As was mentioned in comments and recommendations above, the removal of script for jQuery 1.8.2 file resolved my problem. Thank you to all :)!

发布评论

评论列表(0)

  1. 暂无评论