wp_enqueue_style和rel而不是样式表? [英] wp_enqueue_style and rel other than stylesheet?

查看:93
本文介绍了wp_enqueue_style和rel而不是样式表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建(或更好地尝试)用Less创建我的第一个WordPress主题.

I create (or better try) to create my first WordPress theme with Less.

我要做的是在我的functions.php中使用这样的脚本

What I do is to use a script like that into my functions.php

wp_register_style('screen_css', get_bloginfo('template_url') . '/css/screen.less', array(), false, 'screen');
wp_enqueue_style('screen_css');

其结果是:

<link rel='stylesheet' id='stigma_screen-css'  href='http://www.stigmahost.dch/wp-content/themes/stigmahost/css/screen.less?ver=1.0' type='text/css' media='screen' />

问题是,当我使用wp_register_style()函数时,能否以某种方式更改rel ="stylesheet"吗?

The question is, can I change somehow the rel="stylesheet" when I using the wp_register_style() function ?

推荐答案

虽然这两个函数都不会让您传递该值,但是在使用style_loader_tag过滤器呈现标记之前,您确实可以访问该标记.如果你做这样的事...

While neither function will let you pass that value in, you do have access to the tag before it is rendered with the style_loader_tag filter. If you do something like this...

add_filter('style_loader_tag', 'my_style_loader_tag_function');

function my_style_loader_tag_function($tag){
  //do stuff here to find and replace the rel attribute

  return $tag;
}

...您应该可以将rel属性替换为所需的任何内容.请记住,此过滤器会将整个标记作为html传递,因此您必须执行preg_replace()或类似的操作以将值替换为所需的值.另外,此过滤器将在每次将样式表放入队列时运行,因此请确保在更改rel属性之前,测试您是否拥有正确的样式表(使用preg_match()或其他东西).

...you should be able to replace the rel attribute with whatever you want. Keep in mind that this filter will pass in the whole tag as html, so you'll have to do a preg_replace() or something similar to replace the value with what you want. Also, this filter will run every time you enqueue a stylesheet, so make sure you test that you've got the right one (with a preg_match() or something) before you alter the rel attribute.

这篇关于wp_enqueue_style和rel而不是样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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