2014-10-27 57 views
2

我將bootstrap主題轉換爲wordpress。現在,我在加載引導腳本和樣式時遇到了問題。 這是我的functions.php如何在functions.php(wordpress)中加載引導腳本和樣式?

function wpbootstrap_scripts_with_jquery() 
    { 

     // Register the script like this for a theme: 
     wp_register_script('custom-script', get_template_directory_uri() . '/js/bootstrap.js', array('jquery')); 
     wp_enqueue_script('samplejs', get_template_directory_uri() . '/js/jquery.min.js', array('')); 
      wp_enqueue_script('samplejs', get_template_directory_uri() . '/js/clean-blog.min.js', array('')); 
     // For either a plugin or a theme, you can then enqueue the script: 
     wp_enqueue_script('custom-script'); 
    } 

add_action('wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery'); 

代碼,我加入這在我的頭

<?php wp_enqueue_script("jquery"); ?> 

其他問題: 是什麼wp_register和wp_enqueue之間的區別? 請幫幫我。 Im初學者使用bootstrap。

回答

5

所有你需要做的就是排隊他們。另外,我建議擺脫你的jQuery導入(第二個wp_enqueue)。 WordPress默認包含jQuery,並且您已經將它包含在第一個腳本中(因爲您已將其列爲依賴項)。下面是一個例子,但這排入jQuery的兩倍(本地jQuery和引導jQuery的):

function wpbootstrap_scripts_with_jquery() 
{ 
    // Register the script like this for a theme: 
    wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/bootstrap.js', array('jquery')); 
    wp_enqueue_script('bootstrap-jquery', get_stylesheet_directory_uri() . '/js/jquery.min.js'); 
    wp_enqueue_script('blog-scripts', get_stylesheet_directory_uri() . '/js/clean-blog.min.js'); 
} 

add_action('wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery'); 

你需要註冊腳本前排隊-ING他們來說,是如果其中一個腳本的唯一原因是一種依賴性。從文檔:

wp_register_script()註冊在WordPress腳本文件稍後將使用該wp_enqueue_script()函數,它安全地 處理腳本依賴鏈接 到頁面。

已使用wp_register_script預註冊腳本()做 並不需要使用手動wp_enqueue_script(),如果他們被列爲正在排隊的另一個腳本的依賴 排隊。 WordPress 將在包含 排隊腳本之前自動包含註冊腳本,該腳本將註冊腳本的句柄列爲 依賴項。

+0

非常感謝你@rnevius :)怎麼樣的風格? – akselrows 2014-10-27 09:02:58

+0

我用更多的解釋編輯了我的答案。對於enqueueing樣式,可以使用'wp_enqueue_style'在相同的函數中以相同的方式包含它們。 – rnevius 2014-10-27 09:04:04

+0

嗯。我嘗試了代碼,但效果仍然不起作用 – akselrows 2014-10-27 09:06:30

2

您使用「wp_enqueue_style」和「wp_enqueue_script」。

例如:

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'); 
+0

謝謝@elnaz :) actulally它的工作現在,但由於jQuery衝突,聯繫表單7不工作。請幫我解決這個問題。 – akselrows 2014-10-27 09:26:48

相關問題