0

我正在编写一个具有自定义背景的 Wordpress 主题,但在 WP 文档中,没有关于检索每个背景参数(图像、颜色、位置等)值的函数的get_background_color内容get_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
}

编辑:这些是我需要得到的值。此屏幕截图来自外观 -> 背景。

外观 -> 背景

4

2 回答 2

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' );

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

于 2013-01-30T14:55:33.507 回答
0

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

于 2012-06-19T09:23:00.050 回答