2013-11-01 76 views
1

我想添加jquery到我的WordPress主題。我已使用此代碼爲什麼腳本不能顯示在WordPress的頁腳部分?

function theme_name_scripts() { 

    wp_enqueue_script('jquery',true); 
} 

add_action('wp_enqueue_scripts', 'theme_name_scripts'); 

但它顯示在頭部分。不在頁腳部分。是什麼原因?

回答

0

嘗試更換:

wp_enqueue_script('jquery',true); 

有了這個

wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.js', false, null, true); 
wp_enqueue_script('jquery'); 

所有這一切說的是載入名爲jQuery的的JS文件,可以在你的主題文件夾中的「JS」文件夾中找到。

雖然我總是在谷歌的CDN版本加載爲在這裏推薦:

編輯

或者,你可以嘗試把一些在函數行的結尾......這樣的:

wp_enqueue_script('jquery',true,11); 

編輯 也許嘗試這個辦法:How can I include Jquery in my Wordpress footer?

+0

我已經使用你的代碼。但沒有改變 – Bir

0

爲什麼腳本不能顯示在WordPress的頁腳部分?

因爲它已經註冊爲header,默認情況下,WordPress這樣做。

您可以使用(一個簡單的解決方法)

wp_enqueue_script('jquery','/wp-includes/js/jquery/jquery.js','','',true); 

像你這樣不能省略可選參數在這裏wp_enqueue_script('jquery',true);及以下將無法正常工作

wp_enqueue_script('jquery','','','',true); 

還記得,這($in_footer,將腳本放在頁腳中)需要主題將wp_footer()模板標記放在適當的位置。閱讀this article

此外,您還可以使用

wp_deregister_script('jquery'); 
wp_register_script(
    'jquery', 
    // you can use "http://code.jquery.com/jquery-latest.min.js" for latest version 
    /wp-includes/js/jquery/jquery.js, 
    false, 
    false, 
    true 
); 
wp_enqueue_script('jquery'); 

此外,檢查this generator,它給我的選擇產生下面的代碼,我選擇

// Register Script 
function custom_scripts() { 
    wp_deregister_script('jquery'); 
    wp_register_script('jquery', 'http://code.jquery.com/jquery-latest.min.js', false, false, true); 
    wp_enqueue_script('jquery'); 
} 
// Hook into the 'wp_enqueue_scripts' action 
add_action('wp_enqueue_scripts', 'custom_scripts'); 

您可以使用此工具非常生成自己的代碼容易。