简体   繁体   English

Wordpress插件CSS顺序

[英]Wordpress Plugin CSS Order

I think I've lost my mind... 我想我忘了...

I wrote a WP plugin a couple of years ago. 我几年前写了一个WP插件。 It consists of a couple of widgets, some short code, and code for a complete page. 它由几个小部件,一些短代码以及一个完整页面的代码组成。 No rocket science, really. 真的,没有火箭科学。

I'm doing a quick rewrite and have started running into a problem where the CSS for site's theme is being loaded AFTER the plugin CSS. 我正在快速重写,已经开始遇到一个问题,即在插件CSS之后加载网站主题的CSS。 Of course, this makes it imposible to fine tune the plugin. 当然,这使得微调插件成为不可能。

Why on earth would it do that? 为什么在地球上会这样做? Here's the code for enqueing the css. 这是使CSS入队的代码。

**wp_enqueue_style("aa_remote", "http://domaind.com/css/site.css", false, "1.0");**

It has worked for two years! 它已经工作了两年! and now it doesn't. 现在没有。

I did some digging and found that I may be able to use a dependancy, so I loaded the theme's stylesheet like this. 我进行了一些挖掘,发现我可以使用依赖项,因此我像这样加载了主题的样式表。

**wp_enqueue_style("aa_remote", "http://domaind.com/css/site.css", (array) get_stylesheet_uri(), "1.0");**

But that failed miserably. 但这失败了。

I do a some hair left but I'm afraid it is in peril! 我还剩下一些头发,但恐怕是危险的!

I'm sure I'm missing something basic... Any/All suggestions appreciated! 我确定我缺少基本的知识...任何/所有建议都值得赞赏! (walking away insn't an option!). (走走不是一个选择!)。

Another approach you can take is to utilize the wp_enqueue_scripts hook/function to set a low priority for your stylesheet being processed. 您可以采用的另一种方法是利用wp_enqueue_scripts钩子/函数为正在处理的样式表设置低优先级。 This way you can guarantee that styles load after all the default ones within WordPress plugin's code: 这样,您可以确保样式加载在WordPress插件代码中的所有默认样式之后:

 function my_plugin_unique_style() {
     $base = get_stylesheet_directory_uri();
     wp_enqueue_style( 'style-my-plugin-style', $base.'/styles.css' );
 }
 add_action('wp_enqueue_scripts', 'my_plugin_unique_style', 11 );

Of course you will have to modify this to use your plugin's css file name but this is a way to control the order your site's styles are loading. 当然,您将必须对其进行修改以使用插件的CSS文件名,但这是控制网站样式加载顺序的一种方式。 It's worth mentioning that if this still loads before another CSS file in the HEAD of the page you should bump up the number from 11 to some other higher number to move it down in priority and load later. 值得一提的是,如果仍然在页面的HEAD中加载另一个CSS文件之前,则应将数字从11增大到其他更高的数字,以将其优先级降低并稍后加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM