星期一, 8月 01, 2016

[Wordpress] wp_enqueue_script / wp_enqueue_style 在wp自訂你的js與css檔

wp_enqueue_script 允許我們快速的在wp的頁面加入js函式庫,
反之 wp_enqueue_style讓我們加入樣式檔。

你可以寫法functions.php做全域的載入或至到外掛模組佈景主題裡面做客製化的載入唷

範例

以下範例教你怎麼加入bootstrap框架

function reg_scripts() {
    wp_enqueue_style( 'bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css' );
    wp_enqueue_style( 'bootstrapthemestyle', get_template_directory_uri() . '/css/bootstrap-theme.min.css' );
    wp_enqueue_script( 'bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true );
}
add_action('wp_enqueue_scripts', 'reg_scripts');

參考


  1. https://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/
  2. http://stackoverflow.com/questions/26583978/how-to-load-bootstrap-script-and-style-in-functions-php-wordpress
  3. https://codex.wordpress.org/zh-cn:%E5%87%BD%E6%95%B0%E5%8F%82%E8%80%83/wp_enqueue_script

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails