wp_enqueue_style和rel而不是样式表?

时间:2011-11-10 15:48:25

标签: css wordpress wordpress-theming less

我创建(或更好地尝试)使用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”吗?

2 个答案:

答案 0 :(得分:7)

虽然这两个函数都不允许您传递该值,但在使用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()或其他内容)。

答案 1 :(得分:4)

我知道这是一个古老的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);
}
相关问题