php - WordPress 排队脚本和样式

标签 php jquery css wordpress

我使用开源 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: Loading multiple scripts with enqueue

最佳答案

在函数中使用此代码。在函数之前

 if ( ! function_exists( 'smarter_scripts' ) ) {

然后在关闭此 if 条件之前放置此代码。

add_action( 'wp_enqueue_scripts', 'smarter_scripts', 99);

在 header.php 文件中,将代码放在标记之前

<?php wp_head(); ?>

关于php - WordPress 排队脚本和样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144888/

相关文章:

javascript - 如何获取元素的父元素?

Javascript 生成 HTML 代码调用 knockout

html - 动画返回 css 动画

php - 如何编写php函数

Java:用什么数据结构来模仿PHP的关联数组?

ruby-on-rails - 使用 Rails 3 和 Jquery 实现无限滚动

jQuery UI 小部件(即 slider )并触发其自定义事件处理程序

jquery - 响应式设计,网站在手机上不滚动

CSS3 动画 : How to have the object not revert to its initial position after animation has run?

php - Apache mod_rewrite : get rewritten directories as array and maintain the query string