1

我有多个 javascript 文件。如何通过 wp_register_script 将这些添加到 wordpress ?我可以通过添加单个 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' );
4

1 回答 1

4

您可以为此创建一个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' );
于 2013-08-19T05:23:37.377 回答