[英]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測試功能的模板文件(我得到“否”):
<?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()
。
您可以查看以下鏈接以獲取已包含庫的列表。
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
您應該在頁面模板中使用get_header()
,以便在頁面中獲取標題代碼(包括排隊的腳本)。 而且您可能也想使用get_footer()
。 頁腳中包含一些腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.