我目前正在努力提高我的主题开发技能,在php方面还有很多需要学习的地方。
我正在调查Underscores 和Twenty Seventeen 主题。我只是不明白为什么下面的代码可以工作,以及它隐藏了什么Site Title 和Tagline, 如果Display Site Title and Tagline 未选中Customizer中的复选框。
在里面custom-header.php, 使用默认文本颜色设置自定义标题后#000000, 主题为标题样式设置函数:
function underscores_header_style() {
$header_text_color = get_header_textcolor();
/*
* If no custom options for text are set, let\'s bail.
* get_header_textcolor() options: Any hex value, \'blank\' to hide text. Default: add_theme_support( \'custom-header\' ).
*/
if ( get_theme_support( \'custom-header\', \'default-text-color\' ) === $header_text_color ) {
return;
}
// If we get this far, we have custom styles. Let\'s do this.
?>
<style type="text/css">
如果我现在将标题文本的颜色设置为
#000000 在Customizer中,在我看来
if 条款应为真
return; 应该结束功能,以下隐藏和超越的整个造型部分不应该工作。
到目前为止,我已经与var_dump() 对于get_header_textcolor() 和get_theme_support(\'custom-header\', \'default-text-color\'). 这两个值都是000000.
与我的上述考虑相反:一切正常。
我不想在我的主题中“复制-粘贴”这个问题,我想理解它。我的思维错误在哪里?
最合适的回答,由SO网友:Fayaz 整理而成
首先,当您取消选中Display Site Title and Tagline 在customizer中,您已经输入了什么值并不重要Header Text Color, 自定义程序将其另存为blank (非空字符串,实际文本为“空白”)在您之后Save & Publish. 你可以登记wp_options 数据库中的Save & Publish or 尝试var_dump() 移出自定义程序后。
自从get_header_textcolor() 退货blank 和get_theme_support( \'custom-header\', \'default-text-color\' ) 退货000000, 它们在上述方面不匹配if 条件,所以隐藏工作。
其次,你得到000000 具有var_dump() 对于两者get_header_textcolor() 和get_theme_support( \'custom-header\', \'default-text-color\' ) 当您在customizer中时,因为customizer在保存后不会重新加载页面(&;它使用JavaScript显示/隐藏Title &;Tagline 保存前。如果保存后关闭自定义程序,您将看到var_dump() 将显示blank 对于get_header_textcolor() 和000000 对于get_theme_support( \'custom-header\', \'default-text-color\' ).
这是自定义程序用于将值另存为的行blank 在wp-admin/js/customize-controls.js 文件:
control.setting.set( to ? last : \'blank\' );
此外,在
wp-includes/class-wp-customize-manager.php 文件的
_sanitize_header_textcolor 函数,您将看到WordPress允许
blank 是的有效输入
header_textcolor 以及十六进制格式的任何有效颜色值:
if ( \'blank\' === $color )
return \'blank\';
所以你的逻辑是正确的,但你的值错了。