在特定页面上取消注册WordPress jQuery

时间:2016-08-19 作者:brandozz

Note: Removing jQuery that comes with Wordpress is not recommended. Use caution if implementing the solution below

我有一些页面不需要在标题中加载jQuery。我想知道是否有可能在特定的页面上取消WordPress jQuery的排队/注销和jQuery cdn的排队/注册?

我已经阅读了注意事项,但我们没有使用任何插件,主题是定制的。

下面是我当前的代码,但它似乎不想工作。默认的Wordpress jQuery仍然加载在标题中。

if (!function_exists(\'modify_jquery\')) {
    function modify_jquery() {
        if (is_page(array(\'page 1\', \'page 2\'))) {
            wp_dequeue_script(\'jquery\');
            wp_deregister_script(\'jquery\');

            wp_register_script(\'jquery-custom\', \'//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\', false, \'1.11.3\', \'true\');
            wp_enqueue_script(\'jquery-custom\');
        }
    }
}
add_action(\'init\', \'modify_jquery\');

1 个回复
最合适的回答,由SO网友:CodeMascot 整理而成

挂钩到wp_enqueue_scripts. 它会成功的。

修改后的代码为-

if (!function_exists(\'modify_jquery\')) {
    function modify_jquery() {
        if (is_page(array(\'page 1\', \'page 2\'))) {
            wp_dequeue_script(\'jquery\');
            wp_deregister_script(\'jquery\');

            wp_register_script(\'jquery-custom\', \'//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\', false, \'1.11.3\', \'true\');
            wp_enqueue_script(\'jquery-custom\');
        }
    }
}
// add_action(\'init\', \'modify_jquery\');
add_action(\'wp_enqueue_scripts\', \'modify_jquery\');
希望这能有所帮助。