【问题标题】:How to add media="print" to all css files except style.css如何将 media="print" 添加到除 style.css 之外的所有 css 文件
【发布时间】:2018-03-08 07:30:21
【问题描述】:

过滤特定 css 文件的链接非常简单。但我想过滤除 style.css 之外的所有内容:add_filter( 'style_loader_tag', 'remove_https_http', 10, 4 ); 递归地提供优化的 css 和页面速度和加载时间的高分。

 add_filter( 'style_loader_tag',  'remove_https_http', 10, 4 );
    function remove_https_styles( $html, $handle, $href, $media ){
        $handles = array('twentysixteen-fonts', 'open-sans');
        if( in_array( $handle, $handles ) ){
            $html = str_replace('https:', '', $html);   
        }
        return $html;
    }

如果有其他方法,那就太好了。我已经在学习:CSS delivery optimization: How to defer css loading?。但在我看来,过滤所有可以在渲染我的页面后加载的 css 链接,建议:https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css

任何想法都会是一个很好的工具提示。提前致谢。

【问题讨论】:

  • 这既不是 HTML 也不是 CSS。是PHP吗? WordPress?
  • 更新。谢谢。

标签: wordpress


【解决方案1】:

在主题的functions.php文件中添加如下代码

function theme_styles()  
{ 
  // Register the style like this for a theme:  
  // (First the unique name for the style (custom-style) then the src, 
  // then dependencies and ver no. and media type)
  wp_enqueue_style( 'style-css', get_template_directory_uri() . '/style.css', array(), '1.2', 'print' );
}
add_action('wp_enqueue_scripts', 'theme_styles');

希望得到帮助,如有任何疑问,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 2017-06-17
    • 2011-01-29
    • 2015-03-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多