Wordpress 3.4的自定义背景支持功能?

时间:2012-06-19 09:06:27

标签: wordpress

我正在编写一个具有自定义背景的Wordpress主题,但在WP文档中,没有任何关于检索每个背景参数(图像,颜色,位置等)的值的函数我只想到了get_background_colorget_background_image

这是支持自定义背景的代码:

$custom_background_support = array(
    'default-color'          => 'FFF',
    'default-image'          => '',
    'wp-head-callback'       => 'custom_background_cb'
);

if ( is_wp_version( '3.4' ) )
    add_theme_support( 'custom-background', $custom_background_support ); 
else
    add_custom_background( $custom_background_support );

这就是回调:

function academia_custom_background_cb()
{
?>
<style type="text/css">
body{
background-color: #<?=get_background_color();?> !important;
background-image: url('<?=get_background_image();?>');
background-position: ...
background-repeat: ...
...
}</style>
<?php
}

编辑:这些是我需要获得的价值。此屏幕截图来自Appearance - &gt;背景

Appearance -> Background

2 个答案:

答案 0 :(得分:1)

旧问题,但它在我的谷歌搜索中找到了相同的信息。完成Krike的回答,确实是get_theme_mod()

您可以在默认wp-head-callback中查看它。

/**
 * Default custom background callback.
 *
 * @since 3.0.0
 * @access protected
 */
function _custom_background_cb() {
    // $background is the saved custom image, or the default image.
    $background = set_url_scheme( get_background_image() );

    // $color is the saved custom color.
    // A default has to be specified in style.css. It will not be printed here.
    $color = get_theme_mod( 'background_color' );

    if ( ! $background && ! $color )
        return;

    $style = $color ? "background-color: #$color;" : '';

    if ( $background ) {
        $image = " background-image: url('$background');";

        $repeat = get_theme_mod( 'background_repeat', 'repeat' );
        if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
            $repeat = 'repeat';
        $repeat = " background-repeat: $repeat;";

        $position = get_theme_mod( 'background_position_x', 'left' );
        if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
            $position = 'left';
        $position = " background-position: top $position;";

        $attachment = get_theme_mod( 'background_attachment', 'scroll' );
        if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
            $attachment = 'scroll';
        $attachment = " background-attachment: $attachment;";

        $style .= $image . $repeat . $position . $attachment;
    }
?>
<style type="text/css" id="custom-background-css">
body.custom-background { <?php echo trim( $style ); ?> }
</style>
<?php
}

所以你会得到重复,位置和依恋:

$repeat = get_theme_mod( 'background_repeat', 'repeat' ); 
$position = get_theme_mod( 'background_position_x', 'left' ); 
$attachment = get_theme_mod( 'background_attachment', 'scroll' );

我假设第二个参数是默认值。

答案 1 :(得分:0)

我不是100%肯定,但我认为您正在寻找get_theme_mod() - &gt; http://codex.wordpress.org/Function_Reference/get_theme_mod