如何向 WordPress 新增一個簡單的 jQuery 指令碼?


WordPress 是一個用於開發動態網站的開源 CMS。下面讓我們學習如何向 WordPress 新增 jQuery 指令碼。在 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.