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 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题