这个将脚本和样式排入主题队列的函数有什么问题?

时间:2017-07-14 作者:Vishal
     function custom_scripts_css_with_jquery()
{

    //wp_register_script( \'Bootstrap_min_js\',\'//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\' );
    wp_register_script( \'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\',null, null, true);
    wp_register_script( \'select2jquery\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js\',null, null, true );
    wp_register_style( \'select2mincss\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css\', null, null, true );

    // For either a plugin or a theme, you can then enqueue the style:
    wp_enqueue_script( \'select2jquery\' );
    wp_enqueue_script( \'jquery\' );
    wp_enqueue_style( \'select2mincss\' );
    //wp_enqueue_script( \'Bootstrap_min_js\');


}
add_action( \'wp_enqueue_scripts\', \'custom_scripts_css_with_jquery\' );
1 个回复
最合适的回答,由SO网友:dbeja 整理而成

我唯一想改变的是:

您包含的jQuery版本与WordPress中包含的版本不同。您应该首先取消注册WP版本,这样页面上就不会加载jQuery的两个副本/版本。选择2jQuery应该将jQuery作为依赖项,以便确保在jQuery的第三个参数之后加载wp_register_script 如果未使用,则默认情况下应为空数组wp_register_style 应为已定义此样式表的媒体,默认情况下为“all”(传递的是布尔值true)我将更改为:

function custom_scripts_css_with_jquery() {

    wp_deregister_script(\'jquery\');
    wp_register_script( \'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\', array(), null, true);
    wp_register_script( \'select2jquery\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js\',array(\'jquery\'), null, true );
    wp_register_style( \'select2mincss\', \'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css\' );  // the rest of parameters are the default

    // For either a plugin or a theme, you can then enqueue the style:
    wp_enqueue_script( \'select2jquery\' );
    wp_enqueue_script( \'jquery\' );
    wp_enqueue_style( \'select2mincss\' );
    //wp_enqueue_script( \'Bootstrap_min_js\');

}
add_action( \'wp_enqueue_scripts\', \'custom_scripts_css_with_jquery\' );

结束