简体   繁体   English

WordPress-functions.php将不会注册我的JavaScript文件

[英]WordPress - functions.php will not register my JavaScript file

I am trying to register my JavaScript file in my function.php file. 我正在尝试在我的function.php文件中注册我的JavaScript文件。 But when I add in the function to load the JavaScript file, nothing happens. 但是,当我添加函数以加载JavaScript文件时,什么也没发生。 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: 这是我的PHP代码:

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. 这在我的function.php文件的底部,脚本scripts.js文件位于主题文件夹的js文件夹中。

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. 您缺少/ js路径文件夹之前。 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 . 请小心handlescript 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. 请确保您的ID是唯一的。 first param in wp_enqueue_script. wp_enqueue_script中的第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM