[英]Adding a JS file in a Wordpress block child theme
I am creating a block child theme of Wordpress, I need to attach my own CSS sheet and JS file.我正在创建 Wordpress 的块子主题,我需要附上我自己的 CSS 工作表和 JS 文件。 As recommended, I queued both files in functions.php with:
按照建议,我在 functions.php 中对这两个文件进行了排队:
//dodanie własnych css i js
add_action( 'wp_enqueue_scripts', 'child_enqueue_assets' );
function child_enqueue_assets() {
//css
wp_enqueue_style( 'style', get_stylesheet_uri() );
//js
wp_enqueue_script( 'script', get_template_directory_uri() . '/script.js', array( 'jquery' ),'',true );
}
add_action( 'admin_init', 'child_editor_styles' );
function child_editor_styles() {
add_editor_style( [
get_stylesheet_uri()
] );
}
Why is my console browser showing error 404 - JS file not found in parent theme path and not child theme (get_template_directory_uri (). '/Script.js')?为什么我的控制台浏览器显示错误 404 - 在父主题路径而不是子主题中找不到 JS 文件 (get_template_directory_uri ().'/Script.js')? I put the JS file in the child theme and hence I want it to be read, not from the parent theme location.
我将 JS 文件放在子主题中,因此我希望它被读取,而不是从父主题位置读取。 The previously queued patch CSS file is read from the child theme.
从子主题中读取先前排队的补丁 CSS 文件。 What am I doing wrong with the JS file?
我对 JS 文件做错了什么?
The get_template_directory_uri()
will always return the current parent theme url. get_template_directory_uri()
将始终返回当前父主题 url。
To get the child theme URI instead, you should be using get_stylesheet_directory_uri()
要获取子主题 URI,您应该使用
get_stylesheet_directory_uri()
In your code replace the following line:在您的代码中替换以下行:
wp_enqueue_script( 'script', get_template_directory_uri() . '/script.js', array( 'jquery' ),'',true );
with和
wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/script.js', array( 'jquery' ),'',true );
I hope this will help you.我希望这能帮到您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.