[英]wp_enqueue_style and rel other than stylesheet?
我创建(或更好地尝试)用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”吗?
虽然这两个函数都不允许您传递该值,但在使用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()或其他内容)。
我知道这是一个古老的q,但它帮我解决了这个问题。 借用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.