WordPress排队脚本和样式 [英] WordPress enqueue scripts and styles

查看:94
本文介绍了WordPress排队脚本和样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用开源WordPress主题,并希望从header.php文件中删除样式和脚本,并正确编写它们.我已将以下代码添加到我的functions.php文件中,什么也没发生.

I using an open source WordPress theme and want to remove the styles and scripts from header.php file and write them properly. I've added the following code to my functions.php file and nothing happens.

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文件中,以使插件正常工作.有谁知道为什么会这样吗? (我不是主题开发人员,但我正在尝试修复公司正在使用的主题.)

Also, I can't figure out why when I add plugins, the CSS and JS files do not automatically load in the theme. I have to manually add the files to the header.php or footer.php files in order for the plugins to work. Does anyone know why this happens? (I am not a theme developer but I'm trying to fix a theme that my company is using.)

参考:

WordPress:使用队列入载多个脚本

推荐答案

在函数中使用此代码.在函数

In function use this code.before function

 if ( ! function_exists( 'smarter_scripts' ) ) {

然后在条件条件下放置此代码之前将其关闭.

Then before closing this if condition place this code.

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

在您的header.php文件中,代码位于标记之前的代码之下

In your header.php file place below code before tag

<?php wp_head(); ?>

这篇关于WordPress排队脚本和样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆