wp_enqueue_style 和 rel 除了样式表之外?
我创建(或者更好地尝试)使用 Less 创建我的第一个 WordPress 主题。
我所做的就是在我的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" 吗?
I create (or better try) to create my first WordPress theme with Less.
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');
and the result is that:
<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' />
The question is, can I change somehow the rel="stylesheet" when I using the wp_register_style() function ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然这两个函数都不允许您传递该值,但您确实可以在使用 style_loader_tag 过滤器渲染标签之前访问该标签。如果你做这样的事情......
你应该能够用你想要的任何东西替换 rel 属性。请记住,此过滤器会将整个标记作为 html 传递,因此您必须执行 preg_replace() 或类似的操作来将值替换为您想要的值。另外,每次您将样式表排入队列时,此过滤器都会运行,因此在更改 rel 属性之前,请确保测试您是否拥有正确的样式表(使用 preg_match() 或其他方法)。
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...
...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.
我知道这是一个老问题,但它帮助我解决了这个问题。借用brandwaffle的答案,这是我使用的完整功能:
i know this is an old q, but it helped me figure this out. borrowing from brandwaffle's answer here's the full function I used: