我正在开发一个主题,正在与WP_Customize_Color_Control
.
有时它在颜色的十六进制之前包含#符号,有时不包含。我已经对代码进行了三次检查,看不出原因header_textcolor
是在生成的样式表中,在十六进制值之前省略#符号的唯一颜色(这会阻止使用它)。
摘自函数。php:
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_backgroundcolor\', array(
\'label\' => __( \'Header Background Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_backgroundcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_brandtextcolor\', array(
\'label\' => __( \'Header Brand Text Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_brandtextcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_brandhovertextcolor\', array(
\'label\' => __( \'Header Brand Hover Text Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_brandhovertextcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_textcolor\', array(
\'label\' => __( \'Header Text Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_textcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_hovertextcolor\', array(
\'label\' => __( \'Header Hover Text Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_hovertextcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_activetextcolor\', array(
\'label\' => __( \'Header Active Text Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_activetextcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'backgroundcolor\', array(
\'label\' => __( \'Background Color\' ),
\'section\' => \'colors\',
\'settings\' => \'backgroundcolor\',
) ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'footer_backgroundcolor\', array(
\'label\' => __( \'Footer Background Color\' ),
\'section\' => \'colors\',
\'settings\' => \'footer_backgroundcolor\',
) ) );
摘自动态CSS:
body {
background-color: <?= get_theme_mod( \'backgroundcolor\', \'#dddddd\' ); ?>;
}
.navbar-default {
background-color: <?= get_theme_mod( \'header_backgroundcolor\', \'#f8f8f8\' ); ?>;
}
.navbar-default .navbar-brand {
color: <?= get_theme_mod( \'header_brandtextcolor\', \'#777777\' ); ?>;
}
.navbar-default .navbar-brand:hover,
.navbar-default .navbar-brand:focus {
color: <?= get_theme_mod( \'header_brandhovertextcolor\', \'#5e5e5e\' ); ?>;
}
.navbar-default .navbar-nav > .active > a,
.navbar-default .navbar-nav > .active > a:hover,
.navbar-default .navbar-nav > .active > a:focus {
color: <?= get_theme_mod( \'header_activetextcolor\', \'#555555\' ); ?>;
}
.navbar-default .navbar-nav > li > a {
color: <?= get_theme_mod( \'header_textcolor\', \'#777777\' ); ?>;
}
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
color: <?= get_theme_mod( \'header_hovertextcolor\', \'#333333\' ); ?>;
}
由于两个##符号或无#符号阻止设置值,我当前的解决方法是:
.navbar-default .navbar-nav > li > a {
color: <?= get_theme_mod( \'header_textcolor\', \'#777777\' ); ?>;
}
.navbar-default .navbar-nav > li > a {
color: #<?= get_theme_mod( \'header_textcolor\', \'#777777\' ); ?>;
}
然而,这里肯定有问题。其他颜色设置工作正常。
EDIT:
将“header\\u textcolor”更改为任何其他名称(“测试中的foobar”和“navbar\\u textcolor”)也解决了此问题。
我认为“header\\u textcolor”是一个保留值,或者我以某种方式破坏了Wordpress的安装。
最合适的回答,由SO网友:CompactCode 整理而成
我目前无法为您写下所有内容,但您始终可以use a sanitizer (Wordpress有一个内置)添加#,如果它不存在。而且据我所知,你总是需要创建一个设置和一个控件。下面是:
$wp_customize->add_setting(\'Some_color\', array(
\'default\' => \'#fffff\',
\'sanitize_callback\' => \'sanitize_hex_color\',
));
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
\'Some_color\',
array(
\'label\' => __(\'Some_color\', \'DesignitMultistore\'),
\'section\' => \'Some Section\',
\'priority\' => 1,
)
));
sanitize\\u hex\\u color中的代码:
Codelink wordpress因此,您转换了一个示例:
$wp_customize->add_setting(\'header_backgroundcolor\', array(
\'default\' => \'#f8f8f8\',
\'sanitize_callback\' => \'sanitize_hex_color\',
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'header_backgroundcolor\', array(
\'label\' => __( \'Header Background Color\' ),
\'section\' => \'colors\',
\'settings\' => \'header_backgroundcolor\',
) ) );
另外,您是否正在使用Less或Sass,或者您是否正在使用html样式?