在 Wordpress 主题模板中排队 jQuery 插件
Enqueueing jQuery plugin in Wordpress theme template
我制作了一个名为 test-stuff.php 的模板,并尝试在其中使用 jQuery 和 jQuery 验证插件。我已将脚本放入我的 functions.php 文件中,并且 jQuery 在其他常规页面上运行良好,但在我的模板中却不行。
有人可以指出我哪里出错了吗?我是否必须为模板单独排队 jquery 和 jquery 插件?如果我遗漏了一些细节,请询问,我会提供。提前致谢。
functions.php排队部分:
function my_custom_queue() {
wp_enqueue_script('jquery');
wp_enqueue_script( 'validatejq', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js', array( 'jquery' ), '1' );
}
add_action( 'wp_enqueue_scripts', 'my_custom_queue' );
具有jquery测试功能的模板文件(我得到"no"):
<?php
/* Template name: Test stuff */
?>
<script>
window.onload = function() {
/* test */
if (window.jQuery) {
alert("Yes");
} else {
alert("No");
}
};
</script>
据我所知,不用入队,WordPress已经入队了。检查一些已经包含的 JS 库。您还没有调用 get_header()
和 get_footer()
。
您可以查看以下内容link 以获取已包含库的列表。
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
您应该在您的页面模板中使用 get_header()
,以便在您的页面中获取 header 代码(包括排队的脚本)。您可能也想使用 get_footer()
。页脚中包含一些脚本。
我制作了一个名为 test-stuff.php 的模板,并尝试在其中使用 jQuery 和 jQuery 验证插件。我已将脚本放入我的 functions.php 文件中,并且 jQuery 在其他常规页面上运行良好,但在我的模板中却不行。 有人可以指出我哪里出错了吗?我是否必须为模板单独排队 jquery 和 jquery 插件?如果我遗漏了一些细节,请询问,我会提供。提前致谢。
functions.php排队部分:
function my_custom_queue() {
wp_enqueue_script('jquery');
wp_enqueue_script( 'validatejq', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js', array( 'jquery' ), '1' );
}
add_action( 'wp_enqueue_scripts', 'my_custom_queue' );
具有jquery测试功能的模板文件(我得到"no"):
<?php
/* Template name: Test stuff */
?>
<script>
window.onload = function() {
/* test */
if (window.jQuery) {
alert("Yes");
} else {
alert("No");
}
};
</script>
据我所知,不用入队,WordPress已经入队了。检查一些已经包含的 JS 库。您还没有调用 get_header()
和 get_footer()
。
您可以查看以下内容link 以获取已包含库的列表。
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
您应该在您的页面模板中使用 get_header()
,以便在您的页面中获取 header 代码(包括排队的脚本)。您可能也想使用 get_footer()
。页脚中包含一些脚本。