在页脚上的Wordpress jquery

Gie*_*est 3 php wordpress jquery

在这个文件中

WP-包括/脚本loader.php

有这个代码:

$scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.11.3');
$scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.11.3');
$scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.2.1');
Run Code Online (Sandbox Code Playgroud)

如何将jquery放入页脚?

我尝试添加第五个参数"true"或"1",但不起作用.

...
 * Localizes some of them.
 * args order: $scripts->add( 'handle', 'url', 'dependencies', 'query-string', 1 );
 * when last arg === 1 queues the script for the footer
...
Run Code Online (Sandbox Code Playgroud)

我想将jquery放在底部,因为它阻止了正确的页面加载(谷歌页面速度推荐).

Igo*_*ych 8

你必须先将它出列然后再将它排队.以下代码将完全满足您的需求.

function jquery_mumbo_jumbo()
{
    wp_dequeue_script('jquery');
    wp_dequeue_script('jquery-core');
    wp_dequeue_script('jquery-migrate');
    wp_enqueue_script('jquery', false, array(), false, true);
    wp_enqueue_script('jquery-core', false, array(), false, true);
    wp_enqueue_script('jquery-migrate', false, array(), false, true);
}
add_action('wp_enqueue_scripts', 'jquery_mumbo_jumbo');
Run Code Online (Sandbox Code Playgroud)


小智 5

在经历了所有这些问题并亲自测试了解决方法之后,我决定一次迁移所有脚本。

那么如何做得更好,并有一些代码可以将您现在和将来的所有脚本文件加载到页脚中。防止这种麻烦重复发生。也方便您添加任何插件。

打开您的functions.php文件并添加这个坏男孩

// Script to move all Head scripts to the Footer

function remove_head_scripts() { 
   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', 'remove_head_scripts' );

// END of ball ache
Run Code Online (Sandbox Code Playgroud)