【问题标题】:Set WP custombackground color on theme load在主题加载时设置 WP 自定义背景颜色
【发布时间】:2013-03-11 23:30:02
【问题描述】:

我正在尝试获取我的 Wordpress 212 子主题,以确保在激活主题时背景为白色。我不能只将 CSS 添加到 wp_head,因为这样用户就无法设置自定义背景。

我一直在研究 after_setup_theme 钩子

 function set_white_bg() {
 set_theme_mod( custom-background, 'ffffff');   }
 add_action('after_setup_theme', 'set_white_bg');

我也尝试过 - 再次没有运气

function wpse64373_set_custom_background( $new_colour )
{
$old_colour = get_theme_mod(
     'background_color'
    ,get_theme_support( 'custom-background', 'default-color' )
);

  if ( $old_colour == $new_colour )
    return;

return set_theme_mod( 'background_color', $new_colour );
}

function wpse64373_update_custom_background()
{ wpse64373_set_custom_background( 'ffffff' ); }
 add_action( 'switch_theme', 'wpse64373_update_custom_background' );

我在这里做错了什么?

【问题讨论】:

    标签: php wordpress colors


    【解决方案1】:

    尝试将其添加到您的子主题的function.php中

    add_action('after_setup_theme','child_default_background_color');
    function child_default_background_color() {
        $theme_options = get_option( 'twentytwelve_theme_options');
        if ( 'dark' == $theme_options['color_scheme'] )
            $default_background_color = 'ffffff';
        else
            $default_background_color = '333333';
    
        add_theme_support( 'custom-background', array(
            // set default background color in child theme
            'default-color' => $default_background_color
        ) );
    }
    

    【讨论】:

    • 谢谢,但它不起作用。背景保持与以前相同的颜色。
    猜你喜欢
    • 2018-06-19
    • 2012-07-31
    • 2020-12-14
    • 2020-01-06
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2020-02-22
    相关资源
    最近更新 更多