wordpress customizer get_theme_mod没有输出

时间:2016-08-17 08:05:53

标签: php html css wordpress

我正在研究wordpress的主题,看看它是如何工作的等等。但现在我想实现一些自定义设置/控件。所以这就是我试过的

    function myfirsttheme_customizer_register($wp_customize){
        $wp_customize->add_section('mycustomtheme_colors', array(
        'title' => __('Colors','mycustomtheme'),
        'description' => 'Modify the theme colors'
        )); 

        $wp_customize->add_setting('background_color', array(
        'default' => '#fff',
        )); 

        $wp_customize->add_setting('link_color', array(
        'default' => '#4b4b4',
        ));

        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'background_color', array(
        'label' => __('Edit Background Color', 'mycustomtheme'),
        'section' => 'mycustomtheme_colors',
        'settings' => 'background_color'
        ) ));

        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-color', array(
        'label' => __('Edit Link Color', 'mycustomtheme'),
        'section' => 'mycustomtheme_colors',
        'settings' => 'link_color'
        ) ));
    }
add_action('wp_head','mycustomtheme_css_customizer');
add_action('customize_register','myfirsttheme_customizer_register');

和HTML / CSS

function mycustomtheme_css_customizer(){
    ?>

    <style type="text/css">
        article { background-color:<?php echo get_theme_mod('background_color');?> ; }
    </style>

    <?php
}

因此,当我使用echo get_theme_mod('background_color');这样的实际颜色更改行#fff时,它可以正常工作,但由于某种原因get_theme_mod没有提供输出,我可以&# 39;不明白为什么不。

1 个答案:

答案 0 :(得分:0)

在设置数组中添加这些行

'type' => 'theme_mod', 'capability' => 'edit_theme_options',

以下代码将起作用

 function myfirsttheme_customizer_register($wp_customize){
    $wp_customize->add_section('mycustomtheme_colors', array(
    'title' => __('Colors','mycustomtheme'),
    'description' => 'Modify the theme colors'
    )); 

    $wp_customize->add_setting('background_color', array(
    'default' => '#fff',
    'type' => 'theme_mod',
    'capability' => 'edit_theme_options'
    )); 

    $wp_customize->add_setting('link_color', array(
    'default' => '#4b4b4',
    'type' => 'theme_mod',
    'capability' => 'edit_theme_options'
    ));

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'background_color', array(
    'label' => __('Edit Background Color', 'mycustomtheme'),
    'section' => 'mycustomtheme_colors',
    'settings' => 'background_color'
    ) ));

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link-color', array(
    'label' => __('Edit Link Color', 'mycustomtheme'),
    'section' => 'mycustomtheme_colors',
    'settings' => 'link_color'
    ) ));
}

add_action('customize_register','myfirsttheme_customizer_register');