【发布时间】:2015-11-15 16:02:17
【问题描述】:
我使用开源 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 文件才能使插件正常工作。有谁知道为什么会这样? (我不是主题开发人员,但我正在尝试修复我公司正在使用的主题。)
参考:
【问题讨论】:
-
wp_enqueue_script()仅在主题实际调用wp_head()函数时有效。您可以检查以确保主题调用它吗?它位于header.php文件中的<head></head>标记之间。 -
那不见了。我添加了它,现在它正在工作。谢谢!