I am trying to register my JavaScript file in my function.php
file. But when I add in the function to load the JavaScript file, nothing happens. When I go into dev tools and inspect it on the browser, it isn't appearing there either. (I am working locally)
Here is my PHP code:
function add_scripts() {
wp_enqueue_script('scripts', get_template_directory_uri() . 'js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');
This is at the bottom of my function.php file, and the file, scripts.js is located in the js folder in my theme folder.
I should at least be seeing this load when i inspect the webpage with my dev tools, but it's not appearing. Does anyone know what might be causing this?
You are missing /
before js path folder. Add follows -
function add_scripts() {
wp_enqueue_script('your_scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');
function theme_enqueue_styles() {
wp_enqueue_script( 'script-js', get_stylesheet_directory_uri() . '/js/script.js', array( 'jquery' ), '20181207',true );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 12 );
This method is tested and working
Just missed out with the '/' in file path. Please be careful with the handle
ie script
. Just copy the code and replace your one.
function add_scripts() {
wp_enqueue_script('scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'add_scripts');
please make sure your id is unique. first param in wp_enqueue_script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.