duanqiao9541 2014-02-20 15:09
浏览 78
已采纳

如何在页脚中加载WordPress提供的jQuery?

Setting the $in_footer parameter to true in wp_enqueue_script loads a script in the footer. This is great if I'm adding a new script. However, WordPress already provides a version of jQuery by default and this is enqueued in the document head.

How can I move the jQuery provided by WordPress from the header to the footer?

  • 写回答

1条回答 默认 最新

  • duanmu2013 2014-02-20 16:56
    关注

    You need to de-register the existing query and re-register it to load in the footer.

    function jquery_in_footer() {
        if (!is_admin()) {
            wp_deregister_script('jquery');
    
            // load the local copy of jQuery in the footer
            wp_register_script('jquery', home_url(trailingslashit(WPINC) . 'js/jquery/jquery.js'), false, null, true);
    
    
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'jquery_in_footer');
    

    Bear in mind that if you have any other scripts enqueued that depend on jQuery and are not loaded in the footer, WordPress will still load jQuery in the header to satisfy that dependency.

    (adapted from http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/)

    Edit: If you really want to get crazy, you could modify the WP_Scripts object directly, but then you're dependent on the implementation never changing. I imagine that would change before they even thought of moving jquery.js' location in wp-includes :)

    But just for fun…

    function load_jquery_footer() {
        global $wp_scripts;
        $wp_scripts->in_footer[] = 'jquery';
    }
    add_action('wp_print_scripts', 'load_jquery_footer');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器