WordPress不加载jQuery

时间:2020-08-15 作者:Mark

我想将WordPress与引导一起使用,但WordPress无法正确加载jQuery,因此我收到以下错误消息:

未捕获的TypeError:引导程序的JavaScript需要jQuery。jQuery必须包含在引导程序的JavaScript之前。

这是我的职能。php:

function add_theme_scripts() {
    wp_enqueue_style(\'bootstrap\', \'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css\');
    wp_enqueue_style(\'style\', get_theme_file_path() . \'/style.css\');
    wp_enqueue_script(\'jquery\');
    wp_enqueue_script(\'Popper\', \'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js\', array(\'jquery\'), null, true);
    wp_enqueue_script(\'Bootstrap\', \'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js\', array(\'jquery\', "Popper"), null, true);
    wp_enqueue_script(\'script\', get_theme_file_path() . \'/assets/js/script.js\', array(\'jquery\', \'Popper\', \'Bootstrap\'), null, true);
}
add_action(\'wp_enqueue_scripts\', \'add_theme_scripts\');
你能帮我解决这个问题吗?提前多谢了

1 个回复
SO网友:Mark

我忘了在交换机中添加默认案例。。。函数中我的add\\u script\\u attributes函数的case语句。php文件。很抱歉,我以前没有看到这个。

从我的功能。php:

function add_script_attributes($tag, $handle) {
    switch($handle) {
        case "Popper":
            return str_replace("src=\'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js\'", "src=\'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js\' integrity=\'sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo\' crossorigin=\'anonymous\'", $tag);
        case "Bootstrap":
            return str_replace("src=\'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js\'", "src=\'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js\' integrity=\'sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI\' crossorigin=\'anonymous\'", $tag);
        default:
            return $tag;
    }
}
add_filter(\'script_loader_tag\', \'add_script_attributes\', 10, 2);