下面的代码使用bloginfo
筛选以向我的博客标题添加一些CSS规则,并将其输出到网站标题,而不影响HTML标题的<title>
标签此解决方案取自here 而且它工作得很好(在Ubuntu中使用Firefox和Android的默认浏览器进行了测试)。我喜欢它,因为我希望只有一个地方可以控制我的子主题,而不改变父主题模板。问题是这里使用的全局变量,正如我所了解的那样,这对WP不好,但正如@gmazzap所建议的那样here, 在函数中使用全局变量不是一个很坏的解决方案。问题是是否必须更改此代码,以便不使用全局变量。如何做到这一点?
我将Wordpress 4.7与“217个孩子”主题结合使用。
$twentyseventeen_child_in_body = false;
function twentyseventeen_child_action_wp_head_finished() {
global $twentyseventeen_child_in_body;
$twentyseventeen_child_in_body = true;
}
add_action( \'wp_head\', \'twentyseventeen_child_action_wp_head_finished\', PHP_INT_MAX );
function twentyseventeen_child_action_wp_footer_started() {
global $twentyseventeen_child_in_body;
$twentyseventeen_child_in_body = false;
}
add_action( \'wp_footer\', \'twentyseventeen_child_action_wp_footer_started\', 0 );
function twentyseventeen_child_filter_bloginfo( $name, $show = null ) {
global $twentyseventeen_child_in_body;
if ( \'name\' == $show && $twentyseventeen_child_in_body ) {
$name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
return "$name";
} else {
return $name;
}
}
add_filter( \'bloginfo\', \'twentyseventeen_child_filter_bloginfo\', 10, 2 );