php - WordPress:使用排队加载多个脚本

标签 php wordpress

我正在尝试将 jQuery 和其他脚本加载到页眉(或者它应该是页脚),并且我有 jQuery 的工作方式,我可以运行一个警告框。

问题是,jquery-2.0.3.min.js 没有加载,我不知道我是否正确地进行了排队。虽然加载了 jquery-1.10.2。而且,其他脚本也没有加载。对于这两个脚本(2.0.3 和其他脚本),最后是:?ver=3.6.1

我还读到将两者加载到一个函数中可能会更好?

因此,我们将不胜感激!

function load_jquery() {
    wp_register_script( 'jquery_script', get_template_directory_uri() . 'js/jquery-2.0.3.min.js', array( 'jquery' ) );
    wp_enqueue_script( 'jquery_script' );
}
add_action( 'init', 'load_jquery' ); // end jQuery

function another() {
    wp_register_script( 'another_script', get_template_directory_uri() . 'js/another.js', array( 'jquery' ) );
    wp_enqueue_script( 'another_script' );

}
add_action( 'init', 'another' );

最佳答案

首先在 wordpress 中默认有 jquery,所以你不必注册它,只需将它排入队列

大多数 jquery ui 库和核心 jquery 文件已经在 wordpress 中注册,因此您只需要使用正确的句柄入队看这里 enqueue script

wp_enqueue_script用于入队脚本,wp_enqueue_style用于入队样式

调用自定义js最好先注册脚本或样式再使用

wp_register_script//注册脚本

wp_register_style//注册样式

然后使用wp_enqueue_scriptwp_enqueue_style入队

这是我网站上整个过程的示例代码片段

function pr_scripts_styles() {

    wp_enqueue_script('jquery'); // just enqueue as its already registered 
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
        wp_enqueue_script( 'comment-reply' );

    /*   REGISTER ALL JS FOR SITE */
    wp_register_script('pr_cycle_all',get_stylesheet_directory_uri().'/js/pr-slider.js');
    wp_register_script('pr_slider',get_stylesheet_directory_uri().'/js/jquery.cycle.all.min.js');
    wp_register_script('pr_validation_engine',get_stylesheet_directory_uri().'/js/jquery.validationEngine-en.js');
    wp_register_script('pr_validation_locale',get_stylesheet_directory_uri().'/js/jquery.validationEngine.js');
    wp_register_script('stylethemes',get_stylesheet_directory_uri().'/js/stylethemes.js');
    wp_register_script('pr-jquery-ui',get_stylesheet_directory_uri().'/js/jquery-ui.js');
    wp_register_script('main-js',get_stylesheet_directory_uri().'/js/main.js');
    wp_register_script('pr-galleriffic',get_stylesheet_directory_uri().'/js/jquery.galleriffic.js');
    wp_register_script('pr-rollover',get_stylesheet_directory_uri().'/js/jquery.opacityrollover.js');
    wp_register_script('pr_colorbox',get_stylesheet_directory_uri().'/js/jquery.colorbox.js');
    wp_register_script('pr_jcarousel_js',get_stylesheet_directory_uri().'/js/jquery.jcarousel.min.js');


    //wp_register_script('google-map-api','https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false');



    /*   REGISTER ALL CSS FOR SITE */
    wp_register_style('pr_woocommerce',get_stylesheet_directory_uri().'/css/_woocommerce.css');
    wp_register_style('pr_mobile',get_stylesheet_directory_uri().'/css/mobile.css');
    wp_register_style('pr_sec_teal_grey',get_stylesheet_directory_uri().'/css/secondary-teal-grey.css');
    wp_register_style('pr_site_options',get_stylesheet_directory_uri().'/css/site-options.css');
    wp_register_style('pr_teal_grey',get_stylesheet_directory_uri().'/css/teal-grey.css');
    wp_register_style('validation_css',get_stylesheet_directory_uri().'/css/validationEngine.jquery.css');
    wp_register_style('galleriffic_css',get_stylesheet_directory_uri().'/css/galleriffic.css');
    wp_register_style('pr_colorbox_style',get_stylesheet_directory_uri().'/css/colorbox.css');
    wp_register_style('pr_jcarousel_css',get_stylesheet_directory_uri().'/css/jcarouselskin.css');



    /*   CALL ALL CSS AND SCRIPTS FOR SITE */
    wp_enqueue_script('pr-jquery-ui');
    wp_enqueue_script('stylethemes');

    wp_enqueue_script('pr_cycle_all');

    wp_enqueue_script('pr_slider','','','',true);
    wp_enqueue_script('pr_validation_engine');
    wp_enqueue_script('pr_validation_locale');
    wp_enqueue_script('google-map-api');
    wp_enqueue_script('main-js');
    wp_enqueue_script('pr-galleriffic');
    wp_enqueue_script('pr-rollover');
    wp_enqueue_script('pr_colorbox');


    wp_enqueue_style( 'pr-style', get_stylesheet_uri(), array(), '2013-07-18' );
    wp_enqueue_style('pr_site_options');
    wp_enqueue_style('pr_woocommerce');
    wp_enqueue_style('pr_mobile');
    wp_enqueue_style('pr_sec_teal_grey');
    wp_enqueue_style('pr_teal_grey');
    wp_enqueue_style('validation_css');
    wp_enqueue_style('galleriffic_css');
    wp_enqueue_style('pr_colorbox_style');
    if(is_single()){
        wp_enqueue_script('pr_jcarousel_js');
        wp_enqueue_style('pr_jcarousel_css');
    }
}
add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );

还记得将您的函数与 wp_enqueue_scripts Hook ,以便正确加载脚本和样式 add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );

关于php - WordPress:使用排队加载多个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19263390/

相关文章:

php - MySQL 选择总和的记录

php - 如何使用 html5 和 mysql 创建多个自动完成字段

html - 右边距和宽度 :100% causes scrollbar to appear in WordPress backend

javascript - 使用水平滚动显示 Wordpress 帖子缩略图

php - 将商标符号添加到整个网站的特定短语

php - MySQL SELECT 语句 : Multiple items and Owner's Information

javascript - 选择html表的一些行,并在单击链接时将所选行的一列的值发送到另一个php页面

wordpress - 尽管禁用了 'Force secure checkout',但 woocommerce 中的 SSL 连接错误

html - 将 CSS 直接插入 HTML 时,悬停伪类不起作用?

jquery - 如何使用 jquery 从没有任何 id 或类的 div 中删除 anchor 标记?