如何將簡單的 jQuery 指令碼新增到 WordPress?


WordPress 是一個開源的 CMS,可用於開發動態網站。讓我們學習如何將 jQuery 指令碼新增到 WordPress 中。在 WordPress 主題資料夾中建立一個名為 "js" 的資料夾,然後新增一個檔案。該檔案將是副檔名為 “.js” 的查詢檔案。將 jQuery 指令碼新增到該檔案中。我們已將其命名為 new.js

這是你的 jQuery 指令碼

jQuery(document).ready(function($) {
  $('#nav a').last().addClass('last');
});

開啟主題的 functions.php 檔案。使用 wp_enqueue_script() 函式,這樣你才能新增你的指令碼。 

以下是程式碼

add_action( 'wp_enqueue_scripts', 'add_my_script' );

function add_my_script() {
   // name your script to attach other scripts and de-register, etc.
   wp_enqueue_script (
    ‘new',
     get_template_directory_uri() . '/js/add_my_script.js',
   
     array('jquery') // this array lists the scripts upon which your script depends
   );
}

假設你的主題在正確的位置擁有 wp_headwp_footer ,那麼這將起作用。 

更新時間: 2019 年 12 月 4 日

263 人檢視

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.