doulanyan6455 2016-08-17 14:04
浏览 42
已采纳

WordPress只将Javascript移动到页脚

Right now I am using a very simple function to move the javascript of WordPress to the footer:

function move_javascripts() {
    remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
    add_action( 'wp_footer', 'wp_enqueue_scripts', 5 );
}

The problem though is that this code seems to bring all the "scripts" to the footer, even the CSS... I've tried to find a way to move only the .js part but I can't find any...

So is there some way to only target the javascripts and move them to the footer without the CSS?

  • 写回答

2条回答 默认 最新

  • douxia6163 2016-08-17 14:09
    关注

    You are pretty much overkilling the whole WordPress Script system with this and to be honest - this is completely, I mean totally wrong. When you enque a script into WordPress you do this by wp_enqueue_script.

    This function takes 5 parameters, of which the last one can be set to true, in order to load it in the footer.

    Here is an excerpt of my functions.php:

    if (!function_exists('wp_template_setup')) {
    
        wp_enqueue_script('modernizr-script', get_template_directory_uri() .   '/js/libs/modernizr.custom.js', '', '', true);
    }
    add_action('wp_enqueue_scripts', 'wp_template_setup');
    

    The last paramter is set to true, which means that it will be loaded in the footer.

    See the WordPress Codex for details: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答