繁体   English   中英

如何在WP主题的页脚中正确加载/移动jquery?

[英]How to properly load/move jquery in footer of WP theme?

我正在尝试将jQuery主题加载到我的WP主题(WP 3.8)的页脚中,但是我尝试的解决方案不适用于我。 我在网上搜索了所有内容,但没有找到在WP主题的页脚中加载jquery的正确方法。

这是我的function.php

if (!is_admin())
    add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);

function my_jquery_enqueue() {

    // Deregister the included library
    wp_deregister_script('jquery');

    // Register the library again from Google's CDN
    wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", false, '1.10.2', true);
    wp_enqueue_script('jquery');
}

而且,在我的function.php ,也可以使用此JS将其正确加载到页脚中:

/* Integrate preloader script */
wp_register_script('jPreloader', get_stylesheet_directory_uri() . '/js/loader.js', 'jquery', null, true);
wp_enqueue_script('jPreloader');

wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/scripts.min.js', array( 'jquery' ), '', true );

/*
    * Register and enqueue a script that does not depend on a JavaScript library.
    */

function child_add_scripts() {

    wp_register_script(
        'avia',
        get_stylesheet_directory_uri() . '/js/avia.js',
        array('jquery'),
        '1.1',
        true
        );

    wp_register_script(
        'bottom',
        get_stylesheet_directory_uri() . '/js/bottom.js',
        array('jquery'),
        null,
        true
        );

    wp_enqueue_script('avia');
    wp_enqueue_script('bottom');
}

// Run this function during the wp_enqueue_scripts action
add_action( 'wp_enqueue_scripts', 'child_add_scripts' );

不知道为什么未在WP主题的页脚中加载jQuery,我在做什么错?

尝试这个:

if (!is_admin())
    add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);

function my_jquery_enqueue() {

    // Deregister the included library
    wp_deregister_script('jquery');

    // Register the library again from Google's CDN
    $googleScript = 'http'. ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . '://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
    wp_register_script('jquery', $googleScript, false, '1.10.2', true);
    wp_enqueue_script('jquery');
}

我已经使用此功能将jQuery移动到页脚:

function custom_clean_head() { 
   remove_action('wp_head', 'wp_print_scripts'); 
   remove_action('wp_head', 'wp_print_head_scripts', 9); 
   remove_action('wp_head', 'wp_enqueue_scripts', 1);

   add_action('wp_footer', 'wp_print_scripts', 5);
   add_action('wp_footer', 'wp_enqueue_scripts', 5);
   add_action('wp_footer', 'wp_print_head_scripts', 5); 
} 

add_action( 'wp_enqueue_scripts', 'custom_clean_head' );

但是,我需要在我的测试站点上的预加载器解决方案: http : //dev.bogosavljev.com/

有人可以看看,然后告诉我我做错了什么吗?

问题是页面上的内容显示了几秒钟,然后显示了我的预加载器(动画字母B),然后带有淡入淡出效果,显示了页面。

如何先显示预加载器(/js/loader.js),然后显示页面? 作为参考,我使用了具有相同效果的Adham网站-http: //www.adhamdannaway.com/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM