wordpress自定义网站背景颜色和图片

functions.php 文件添加如下代码:

// 激活自定义背景和设置回调函数
if ( function_exists( 'add_theme_support' ) ) {
    $defaults = array(
        'default-color' => '000000',//自定义颜色
        'default-image' => get_template_directory_uri() . '/img/background.png',//自定义图片
        'wp-head-callback' => 'my_theme_background_cb',
        'admin-head-callback'    => '',
        'admin-preview-callback' => ''
    );
    add_theme_support( 'custom-background', $defaults );
}
// Callback function to alter custom background behavior
function my_theme_background_cb() {
    $background = get_background_image();
    $color = get_background_color();
 
    if ( ! $background && ! $color )
        return;
 
    $style = $color ?  : '';
 
    if ( $background ) {
        $image = ;
 
        $repeat = get_theme_mod( 'background_repeat', 'repeat' );
        if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
                $repeat = 'repeat';
        $repeat = ;
 
        $position = get_theme_mod( 'background_position_x', 'left' );
        if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
                $position = 'left';
        $position = ;
 
        $attachment = get_theme_mod( 'background_attachment', 'scroll' );
        if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
                $attachment = 'scroll';
        $attachment = ;
 
        $style .= $image . $repeat . $position . $attachment;
    }
?>
    <!-- You can set any class or id target here -->
    <style type= id=>;
        .main { <?php echo trim( $style ); ?> }
    </style>
<?php } ?>

发布日期:

所属分类: Wordpress 综合 标签: