CSS 订购 Wordpress 样式表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7896536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ordering Wordpress Stylesheets?
提问by Jamie
I have a wordpress theme with a stylesheet that needs to be loaded last, since plugin css are interfering with my theme. I was wondering if there was some type of function I could use to make the main stylesheet load last in my theme.
我有一个带有样式表的 wordpress 主题,需要最后加载,因为插件 css 干扰了我的主题。我想知道是否有某种类型的函数可以用来使主样式表在我的主题中最后加载。
回答by kulpae
when you enqueue your stylesheets, use a higher priority, e.g.:
当您将样式表排入队列时,请使用更高的优先级,例如:
add_action( 'wp_enqueue_scripts', array(&$this, 'theme_styles'), 99 );
If some plugins have hooks on 'wp_print_styles', then you have to use it instead, as 'wp_print_styles' will be written after 'wp_enqueue_scripts', iirc.
如果某些插件在 'wp_print_styles' 上有钩子,那么你必须改用它,因为 'wp_print_styles' 将写在 'wp_enqueue_scripts' 之后,iirc。
And as you have complete control over your theme, you also could include your styles directly into header.php, if the hassle with the actions isn't worth time...
由于您可以完全控制您的主题,您还可以将样式直接包含到 header.php 中,如果操作的麻烦不值得花时间...
回答by Mau
wp_print_styles
works better. just add a priority to the call, for example 99.
wp_print_styles
效果更好。只需为呼叫添加优先级,例如 99。
function load_css() {
wp_enqueue_style( 'homepage-css', get_stylesheet_directory_uri() . '/css/homepage-css.css', array(), 0.256, 'all');
}
add_action( 'wp_print_styles', 'load_css', 99 );
回答by chifliiiii
You can always use !important
to override other rules, but I recommend to also be sure that the plugin stylesheets are being inserted properly using the following method. By adding the priority number you can render them at a later time.
您始终可以使用!important
来覆盖其他规则,但我建议还确保使用以下方法正确插入插件样式表。通过添加优先级编号,您可以稍后渲染它们。
Be sure your stylesheets are loading before all your scripts inside the tag of your header.
确保在标题标签内的所有脚本之前加载样式表。
You always need to load stylesheets before scripts and wordpress takes care of that if you use wp_enqueue_styleand wp_enqueue_script
如果您使用wp_enqueue_style和wp_enqueue_script,您总是需要在脚本和 wordpress 之前加载样式表
For example in your functions.php you should use
例如在你的functions.php中你应该使用
add_action('wp_enqueue_scripts', function(){
wp_enqueue_style('main-style','http://yoursite.com/styles/main.css');
}, 99);
Wordpress then will place main.css in your header and giving a 99 priority means it will add it later than the rest (by default the priority of this function it's 10)
Wordpress 然后会将 main.css 放在您的标题中,并赋予 99 优先级意味着它会晚于其余部分添加它(默认情况下,此功能的优先级为 10)
Be sure you got wp_head() in your header file.
确保你的头文件中有 wp_head() 。
Regards
问候
回答by route_66_
You could add '!important' to the end of the css you want to override other classes
您可以将 '!important' 添加到要覆盖其他类的 css 的末尾
example:
例子:
h1 {
color:red !important;
}