wp_enqueue_style 和 rel 除了样式表之外?

发布于 2024-12-14 22:00:24 字数 540 浏览 4 评论 0原文

我创建(或者更好地尝试)使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心欲静而疯不止 2024-12-21 22:00:24

虽然这两个函数都不允许您传递该值,但您确实可以在使用 style_loader_tag 过滤器渲染标签之前访问该标签。如果你做这样的事情......

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() 或其他方法)。

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;
}

...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.

烙印 2024-12-21 22:00:24

我知道这是一个老问题,但它帮助我解决了这个问题。借用brandwaffle的答案,这是我使用的完整功能:

function childtheme_scripts() {

wp_enqueue_style('less',get_stylesheet_directory_uri() .'/style.less');
add_filter('style_loader_tag', 'my_style_loader_tag_function');

wp_enqueue_script('less',get_stylesheet_directory_uri() .'/jscripts/less-1.3.0.min.js', false,'1.3.0');

}
add_action('wp_enqueue_scripts','childtheme_scripts', 1);


function my_style_loader_tag_function($tag){
  //do stuff here to find and replace the rel attribute    
  return preg_replace("/='stylesheet' id='less-css'/", "='stylesheet/less' id='less-css'", $tag);
}

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:

function childtheme_scripts() {

wp_enqueue_style('less',get_stylesheet_directory_uri() .'/style.less');
add_filter('style_loader_tag', 'my_style_loader_tag_function');

wp_enqueue_script('less',get_stylesheet_directory_uri() .'/jscripts/less-1.3.0.min.js', false,'1.3.0');

}
add_action('wp_enqueue_scripts','childtheme_scripts', 1);


function my_style_loader_tag_function($tag){
  //do stuff here to find and replace the rel attribute    
  return preg_replace("/='stylesheet' id='less-css'/", "='stylesheet/less' id='less-css'", $tag);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文