GET_THEME_MOD运行不正常

时间:2014-10-01 作者:AndrettiMilas

代码输出所有内容(样式标记、div类、括号等),但我选择的实际颜色除外。如何显示实际输出(primary\\u color)?

注册自定义颜色选择器:

// WP THEME CUSTOMIZER: COLORS
$colors = array();
$colors[] = array(
    \'slug\'=>\'primary_color\', 
    \'default\' => \'#88C34B\',
    \'label\' => __(\'Primary Color\', \'Ari\')
);
调用颜色:

//// WP THEME CUSTOMIZER: GENERATE CSS
function mytheme_customize_css()
{
    ?>
         <style type="text/css">
             html, body, .container { color:<?php echo get_theme_mod(\'primary_color\'); ?>; }
         </style>
    <?php
}
add_action( \'wp_head\', \'mytheme_customize_css\');
完整代码可在此处找到:

//// WP THEME CUSTOMIZER: GENERATE CSS
function mytheme_customize_css()
{
    ?>
         <style type="text/css">
             html, body, .container { color:<?php echo get_theme_mod(\'primary_color\'); ?>; }
         </style>
    <?php
}
add_action( \'wp_head\', \'mytheme_customize_css\');

// WP THEME CUSTOMIZER: COLORS
$colors = array();
$colors[] = array(
    \'slug\'=>\'primary_color\', 
    \'default\' => \'#88C34B\',
    \'label\' => __(\'Primary Color\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'secondary_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Secondary Color\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'heading_bg_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Heading Background\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'heading_font_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Heading Font\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'heading_links_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Heading Links\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'heading_links_hover_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Heading Links Hover\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'headings_color\', 
    \'default\' => \'#333333\',
    \'label\' => __(\'Headings Color\', \'Ari\')
);
$colors[] = array(
    \'slug\'=>\'background_color\', 
    \'default\' => \'#FFFFFF\',
    \'label\' => __(\'Background Color\', \'Ari\')
);
foreach( $colors as $color ) {
    // SETTINGS
    $wp_customize->add_setting(
        $color[\'slug\'], array(
            \'default\' => $color[\'default\'],
            \'type\' => \'option\', 
            \'capability\' => 
            \'edit_theme_options\'
        )
    );

    // CONTROLS
    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize,
            $color[\'slug\'], 
            array(\'label\' => $color[\'label\'], 
            \'section\' => \'colors\',
            \'settings\' => $color[\'slug\'])
        )
    );
}

// WP THEME CUSTOMIZER: LAST LINE
}
add_action( \'customize_register\', \'wptuts_theme_customizer\', 11 );

1 个回复
最合适的回答,由SO网友:birgire 整理而成

我认为问题就在于这一行:

\'type\' => \'option\', 
您应该删除它,因为默认值为:

\'type\' => \'theme_mod\', 
因为您想使用get_theme_mod().

您还应考虑:

为这些颜色设置段塞添加前缀,使其更加独特\'sanitize_callback\' => \'sanitize_hex_color\', 在您的卫生设置中附言:您的代码片段中缺少一些片段。

奥托编辑:以上答案正确。此外:

当你这样做的时候get_theme_mod(\'primary_color\'), 还应在此处指定默认值,如下所示:get_theme_mod(\'primary_color\', \'#88C34B\'). 这将防止在用户尚未在自定义程序中选择颜色时输出损坏的CSS。

主题模式设置实际上不需要为slug加前缀,因为它们在任何情况下都是唯一存储在主题中的。如果您使用的是选项,而不是主题mods,那么前缀将是可取的。

结束

相关推荐