2013-08-19 45 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');