我正在从文本区域中添加 CSS。在 <head>
中添加时,有没有一种方法可以轻松缩小输出?
CSS
#logo img{
opacity: 0.8;
}
#header{
background-color: red;
}
function add_css_to_head(){
$opt = get_option( 'get_css_head' );
echo '<style type="text/css" id="get_css_head">' . "\n" . stripslashes( $opt ) . "\n" . '</style>';
}
add_action('wp_head', 'add_css_to_head');
我希望在 <head>
中有这样的输出
#logo img{opacity:.8}#header{background-color:red}
提前致谢
最佳答案
你可以结合使用 php heredocs 和 php str_replace...就像这样
function add_css_to_head(){
$buffer = <<<EOD
#logo img{
opacity: 0.8;
}
#header{
background-color: red;
}
EOD;
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
echo($buffer);
}
add_action('wp_head', 'add_css_to_head');
编辑
如果您从主题选项中获取输出,那么代码将如下所示
function add_css_to_head(){
$buffer = get_option( 'get_css_head' );
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
echo($buffer);
}
add_action('wp_head', 'add_css_to_head');
关于css - 添加到 WordPress 头部时缩小输出中的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38721796/