【问题标题】:wp_enqueue_style and rel other than stylesheet?wp_enqueue_style 和 rel 不是样式表?
【发布时间】:2011-12-26 07:16:51
【问题描述】:

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

【问题讨论】:

  • 函数 wp_register_style() 和 wp_enqueue_style() 都不会让你设置 rel 属性,但如果你能提供更多关于你想要完成的事情的信息,也许我可以提供一个解决方法?

标签: css wordpress wordpress-theming less


【解决方案1】:

我知道这是一个旧的 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);
}

【讨论】:

  • less 不需要 jQuery AFAIK,你在依赖数组中有它
【解决方案2】:

虽然这两个函数都不允许您传入该值,但您确实可以在使用 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() 或其他东西)。

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多