现在,wp\\u head中包含的所有内容都在代码视图中左对齐。
而wp\\u头周围的所有其他代码都缩进(选项卡)两次。是否可以在所有wp\\U头部信息中添加缩进/制表符?
谢谢
现在,wp\\u head中包含的所有内容都在代码视图中左对齐。
而wp\\u头周围的所有其他代码都缩进(选项卡)两次。是否可以在所有wp\\U头部信息中添加缩进/制表符?
谢谢
技术上可能,但可能不值得付出努力(和开销)。如果您使用Chrome中的开发人员工具之类的工具检查源代码,您的HTML将自动缩进。事实上,一些缓存插件(如W3 Total Cache)甚至删除了所有空格以缩短页面加载时间。
也就是说,如果您想确保wp_head
如果内容缩进,则需要执行以下操作:
将函数添加到get_header
最后执行的。此函数获取附加到的所有函数wp_head
(使用$wp_filter[\'wp_head\']
), 删除它们,然后将它们重新附加到您自己的自定义操作(my_wp_head
例如)my_wp_head() for example) 然后应附加到wp_head
my_wp_head() 函数创建匹配/替换正则表达式模式的数组($patterns = array("pattern"=>"replace pattern");
). 此模式应修剪空白,并在每行的开头重新添加制表符ob_start() 要捕获输出,请处理上一个wp_head
通过调用函数do_action(\'my_wp_head\')
echo preg_replace( array_keys($patterns), array_values($patterns), ob_get_clean() );
一种更好的方法,避免缩进一些已经缩进的行
function indented_wp_head(){
ob_start();
wp_head();
$header = ob_get_contents();
ob_end_clean();
echo preg_replace("/\\n</", "\\n\\t<", substr($header, 0, -1));
echo "\\n";
}
您可以通过在“functions.php”中创建自定义函数来解决此问题:
function indented_wp_head(){
ob_start();
wp_head();
$header = ob_get_contents();
ob_end_clean();
echo preg_replace("/\\n/", "\\n\\t", substr($header, 0, -1));
echo "\\n";
}
现在只需在“header.php”中调用此函数:<head>
<!-- ... -->
<?php indented_wp_head() ?>
</head>
如果你想用它wp_footer()
您也可以在“functions.php”中创建此函数以供一般使用:/**
* Indents the output of
* a function
*
* @return void
*/
if (! function_exists("print_indented")) {
function print_indented($fn)
{
ob_start();
call_user_func($fn);
$html = ob_get_contents();
ob_end_clean();
echo preg_replace("/\\n/", "\\n\\t", substr($html, 0, - 1));
echo "\\n";
}
}
并在“header.php”或“footer.php”中调用它,如<?php print_indented("wp_footer") ?>
<?php print_indented("wp_head") ?>
使用http://wordpress.org/plugins/wp-minify/. 它将合并css、js和;html+删除无用的评论。您的页面速度将得到提高,源代码中的所有HTML代码都将被压缩。
我想问题解决了!
作为补充,我们可以为需要多个选项卡的html块指定选项卡的数量,并为函数使用参数。
function add_indented($fn, $num_tabs=1, $params=null){
ob_start();
call_user_func($fn, $params);
$html = ob_get_contents();
ob_end_clean();
$tabs="";
for ($i=0 ; $i<$num_tabs ; $i++) $tabs.="\\t";
echo preg_replace("/\\n/", "\\n" . $tabs, substr($html, 0, - 1));
echo "\\n";
}
之前<?php wp_head(); ?>
<?php wp_footer(); ?>
<?php get_template_part(\'template-parts/content\',\'home\'); ?>
之后:<?php print_indented("wp_head"); ?>
<?php print_indented("wp_footer"); ?>
<!-- Add 4 tabs -->
<?php print_indented(\'get_template_part\', 4,\'template-parts/content\', \'home\'); ?>
我看过的每一篇关于这个主题的教程都已经过时了,所以我希望这里的人能帮助我。我想为我的wp登录设置样式。带有CSS文件的php页面。但我想以一种在Wordpress更新时不会被覆盖的方式来做。据我所知,根据我读到的图茨,在我的主题函数中使用函数添加它的最佳方式。php文件。不幸的是,人们建议使用的代码并不适合我。这是我添加的代码。<?php function custom_login() { echo \'<link rel=\"stylesheet\" href=\"wplog