2013-08-19 43 views
1

我有多個JavaScript文件。我怎樣才能通過wp_register_script將這些添加到wordpress中?我可以如何通過wp_register_script添加多個javascript?

function wptuts_scripts_with_jquery() { 
    wp_register_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery')); 

    // For either a plugin or a theme, you can then enqueue the script: 
    wp_enqueue_script('custom-script'); 
} 
add_action('wp_enqueue_scripts', 'wptuts_scripts_with_jquery'); 
+0

重複相同的多個腳本,你不能在一個添加多個呼叫AFAIK。 – elclanrs

+0

謝謝你的回答,但我不清楚你的答案。你能舉個例子嗎? – Alamin

+0

您必須爲要添加的每個腳本重複執行'wp_register_script'和'wp_enqueue_script'。 – elclanrs

回答

4

添加一個JavaScript文件,您可以創建這樣的common function,並調用它的每script一樣,

function wptuts_scripts_with_jquery() 
{ 
    // create array of all scripts 
    $scripts=array('custom-script'=>'/js/custom-script.js', 
        'custom-script1'=>'/js/custom-script1.js', 
        'custom-script2'=>'/js/custom-script2.js'); 
    foreach($scripts as $key=>$sc) 
    { 
     wp_register_script($key, get_template_directory_uri() . $sc, array('jquery')); 
     wp_enqueue_script($key); 
    } 
} 
add_action('wp_enqueue_scripts', 'wptuts_scripts_with_jquery');