2013-06-18 37 views
4

我想通過wp_enqueue_script()加載兩個腳本。我做了功能,但只有第一個加載不是第二個。這裏是代碼:wp_enqueue_script()不加載多個腳本

//Load my own jQuery 
function fix_noconflict() { 
wp_deregister_script('jquery'); 
wp_register_script('jquery' , 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js');} 

add_action('wp_enqueue_scripts' , 'fix_noconflict'); 

//This two functions follow the same 
function mauricio_bootstrap_script_jquery() { 

//Includes bootstrap jQuery 
wp_register_script('custom-script', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap.js', array('jquery')); 

//This enqueus the script 

wp_enqueue_script('custom-script'); 
} 
// Adds the new bootstrap function to the wp_enqueue_scripts 
add_action('wp_enqueue_scripts', 'mauricio_bootstrap_script_jquery'); 

function mauricio_bootstrap_script_carousel() { 

wp_register_script('myscript', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap-carousel.js', array('jquery')); 


wp_enqueue_script('myscript'); 
} 

add_action('wp_enqueue_script', 'mauricio_bootstrap_script_carousel'); 

只是爲了記錄我的頭上有wp_head()。正如我所說的,它加載了第一個包含bootstrap.js的函數。

感謝,

中號

回答

9

爲什麼難道你不嘗試把main函數裏面所有的功能,喜歡這個?

function wpEnqueueScripts(){ 
    // Adds the new bootstrap function to the wp_enqueue_scripts 
    wp_register_script('custom-script', get_template_directory_uri() . '/mauricio_bootstrap/js/bootstrap.js', array('jquery')); 
    wp_enqueue_script('custom-script'); 

    // Adds the new bootstrap function to the wp_enqueue_scripts 
    wp_register_script('myscript', get_template_directory_uri() . '/mauricio_bootstrap/js/bootstrap-carousel.js', array('jquery')); 
    wp_enqueue_script('myscript'); 
}  

add_action('wp_enqueue_scripts', 'wpEnqueueScripts'); 
+1

這是正確的答案。清潔,簡單,並在一個地方。 –

+0

這也很完美!謝謝 – mauricioSanchez

1

有人在此提供WordPress的論壇。這兩個功能合併,增加的動作使用「template_redirect」的時候$標籤是用來代替「wp_enqueue_script」

function mauricio_bootstrap_scripts() { 
wp_register_script('custom-script', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap.js', array('jquery')); 
wp_enqueue_script('custom-script'); 

wp_register_script('myscript', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap-carousel.js', array('jquery')); 
wp_enqueue_script('myscript'); 
} 
add_action('template_redirect', 'mauricio_bootstrap_scriptsl');