简体   繁体   English

如何使用'wp_style_add_data'正确地将样式表排列到Wordpress页面模板?

[英]How to correctly enqueue stylesheets to Wordpress page templates using 'wp_style_add_data'?

I have learned how to use wp_style_add_data to add stylesheets for early versions of internet explorer – which is ideal as it only overwrites the necessary CSS rules and takes the rest from my style.css . 我已经学会了如何使用wp_style_add_data为早期版本的Internet Explorer添加样式表 - 这是理想的,因为它只会覆盖必要的CSS规则并从我的style.css获取其余部分。

In addition to my page.php I have a second page template that I've named full-page.php which is nearly identical, apart from a few changes to the header and a few other small differences. 除了我的page.php我还有一个第二页模板,我将其命名为full-page.php几乎完全相同,除了对标题的一些更改以及一些其他小的差异。

Because my templates are nearly the same, it seems like overkill to write two long stylesheets when I could do something similar to the wp_style_add_data function to only rewrite the ones I need to change. 因为我的模板几乎相同,所以当我可以执行类似于wp_style_add_data函数的操作时,编写两个长样式表似乎有点wp_style_add_data ,只能重写我需要更改的那些。

I had a go at using wp_style_add_data to enqueue the stylesheet for my page template. 我曾经使用wp_style_add_data为我的页面模板排队样式表。 This actually works and takes any styles I add to style.css providing they are not already different ones in full-page.css . 这实际上是有效的,并采用我添加到style.css任何样式,只要它们在full-page.css中不是已经不同的full-page.css

Here is the code I used: 这是我使用的代码:

if (is_page_template('page-templates/full-page.php'))  

    { wp_enqueue_style( 'theme_style', get_stylesheet_uri() );
      wp_enqueue_style( 'theme-full-page', get_stylesheet_directory_uri() . '/css/full-page.css', array( 'theme_style' ) ); 
      wp_style_add_data( 'theme-full-page' );

} 

else { wp_enqueue_style( 'theme_style', get_stylesheet_uri() );

wp_enqueue_style( 'theme_style_ie8', get_stylesheet_directory_uri() . '/css/ie8.css', array( 'theme_style' ) ); 
wp_style_add_data( 'theme_style_ie8', 'conditional', 'IE 8' );

However, it's also coming up with the following error message. 但是,它也会出现以下错误消息。

Warning
: Missing argument 3 for wp_style_add_data(), called in /var/sites/l/lucieaverillphotography.co.uk/public_html/wp-content/themes/lucieaverillphotography/functions.php on line 31 and defined in
/var/sites/l/lucieaverillphotography.co.uk/public_html/wp-includes/functions.wp-styles.php
on line
223

I think this is because I've removed the part that tells it to only take effect if the user is viewing the website on IE 8, but I've been researching what I could add instead, and haven't been able to find an answer. 我想这是因为我已经删除了告诉它的部分只有在用户在IE 8上查看网站时才会生效,但我一直在研究我可以添加的内容,而无法找到回答。

Any help would be greatly appreciated. 任何帮助将不胜感激。

UPDATE: –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– 更新: ------------------------------------------------ --------------------------

I think I've managed to work this out, as it seems to be working now – any changes I make in style.css are affecting the other stylesheet full-page.css , unless I specify something else in full-page.css . 我认为我已经设法解决这个问题,因为它似乎现在正在工作 - 我在style.css中所做的任何更改都会影响其他样式表full-page.css ,除非我在full-page.css指定了其他full-page.css

Looking at the code, would this be the correct/best way to write this out? 看一下代码,这是写出来的正确/最好的方法吗? It seems a little lengthy and I have to repeat the stylesheet names a lot, but it also seems to work. 它似乎有点冗长,我必须重复样式表名称,但它似乎也有效。 I know that there are often different ways of doing things with PHP and I'm wary of causing complications later down the line as I'm building my theme. 我知道通常有不同的方法可以使用PHP,因为我正在构建我的主题,所以我担心会在以后引发并发症。

if (is_page_template('page-templates/full-page.php')) if(is_page_template('page-templates / full-page.php'))

{ wp_enqueue_style( 'theme_style', get_stylesheet_uri() );

  wp_enqueue_style( 'theme-full-page', get_stylesheet_directory_uri() . '/css/full-page.css', array( 'theme_style' ) ); 
  wp_style_add_data( 'theme-full-page', 'title', 'theme-full-page' );
} 

you have already enqueued the style, wp_style_add_data() does not enqueue, it adds meta data to an already enqueued style and this function does not allow for empty parameters. 你已经将样式排入队列,wp_style_add_data()没有入队,它会将元数据添加到已经排队的样式中,并且此函数不允许使用空参数。 see below. 见下文。

if (is_page_template('page-templates/full-page.php')) { 
      wp_enqueue_style( 'lucieaverillphotography_style', get_stylesheet_uri() );
      wp_enqueue_style( 'lucieaverillphotography-full-page', get_stylesheet_directory_uri() . '/css/full-page.css', array( 'lucieaverillphotography_style' ) ); 
      //wp_style_add_data( 'lucieaverillphotography-full-page' );  --> not needed & cannot be used without required inputs

} 

But.....you could combine all your css into a single file and call this as default on all pages (call ie8 specific stylesheets if you want....), this way it can be browser cached and after the 1st visit, your css loads faster...Brush up on CSS specificity and ids/ classes, it will save you a lot of work in the long run! 但是.....你可以将你所有的css组合成一个文件,并在所有页面上将其称为默认值(如果你想要的话,调用ie8特定的样式表),这样它可以被浏览器缓存并在第一次访问后,你的css加载速度更快......刷新CSS特性和ids /类,从长远来看,它将为你节省大量的工作!

If you want template specific changes to css you have a couple of options 如果您想要对css进行特定于模板的更改,您可以选择几个选项

  1. if the css changes are in the article/ page, there will be usually a id/class set unique to that page (or add one yourself). 如果css更改在文章/页面中,则通常会有一个对该页面唯一的ID /类集(或者自己添加一个)。

  2. add custom css directly to the template (it works anywhere in the page, wont be popular with standards but for the moment you can put css rules anywhere inside style tags 将自定义css直接添加到模板中(它可以在页面的任何位置使用,不会受到标准的欢迎,但目前你可以将css规则放在样式标签内的任何地方

  3. Add a custom class into your header for the body tag, eg > 将自定义类添加到body标签的标头中,例如>

You can then specifically target css in your template... eg 然后,您可以专门针对模板中的css ...例如

 .page-id-2 header{ display: none;} //-->use chrome inspector to see what classes are outputted on each template

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

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