我使用开源 WordPress 主题,想要从 header.php 文件中删除样式和脚本并正确编写它们。我已将以下代码添加到我的functions.php 文件中,但没有任何反应。
function smarter_scripts() {
wp_enqueue_style( 'smarter-bootstrap', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' );
wp_enqueue_style( 'smarter-font-awesome', '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css' );
wp_enqueue_style( 'smarter-google-fonts-open-sans', '//fonts.googleapis.com/css?family=Open+Sans:600,400,300,700' );
wp_enqueue_style( 'smarter-google-fonts-josefin', '//fonts.googleapis.com/css?family=Josefin+Sans:400,600' );
wp_enqueue_style( 'smarter-style', get_template_directory_uri(). '/style.css' );
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
//wp_deregister_script('jquery');
//wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', false, '1.11.2');
wp_enqueue_script('jquery');
}
// load a JS file from my theme: js/theme.js
wp_enqueue_script('smarter_script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0', true);
wp_enqueue_script( 'smarter-bootstrap','//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ), true );
}
add_action( 'wp_enqueue_scripts', 'smarter_scripts' );
另外,我不明白为什么当我添加插件时,CSS 和 JS 文件不会自动加载到主题中。我必须手动将文件添加到 header.php 或 footer.php 文件中才能使插件正常工作。有谁知道为什么会发生这种情况? (我不是主题开发人员,但我正在尝试修复我公司正在使用的主题。)
参考:
WordPress:使用队列加载多个脚本