个性化阅读
专注于IT技术分析

wordpress自定义背景主题支持

点击下载

我在WP中使用默认主题的子主题, 并且更改了背景图片(在wp-admin中)…它是通过wp_head()生成的; header.php中的函数…输出源代码如下:

<style type="text/css" id="custom-background-css">
  body.custom-background { background-color: #fffffe; background-image: url('http://example.com/wp-content/uploads/header.jpg'); background-repeat: no-repeat; background-position: top center; background-attachment: fixed; }
</style>

由于某些原因, 我需要修改图像的url(一些钩子, functions.php修改或其他任何东西), 但是我找不到如何执行此操作。我搜索了所有主题文件, 没有任何故障:(

任何人都知道该怎么做(当然, 无需修改wp核心文件-只需修改主题或插件)


#1


老实说, 我不是WordPress的拥护者, 无法为任何主题添加不可调整的功能。这就是我解决的方法。我从body标签中删除了自定义背景类。希望它对某人仍然有用。

function disable_custom_background_css($classes)
{
    $key = array_search('custom-background', $classes, true);
    if($key !== false)
        unset($classes[$key]); 
    return $classes;
}

并且在调用body_class()之前, 最好在functions.php中

add_filter('body_class', 'disable_custom_background_css');

这样可以防止wp_head中的脚本将此特定样式添加到body标签。现在, 你可以决定如何实现背景图像。


#2


WP tuts文章中的代码对我不起作用, 但是我设法进行了一些修改以使它起作用。

global $wp_version;

if ( ! function_exists( 'change_custom_background_cb' ) ) :

function change_custom_background_cb() {
    $background = get_background_image();
    $color = get_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">
.custom-background { <?php echo trim( $style ); ?> }
</style>
<?php
}
if ( version_compare( $wp_version, '3.4', '>=' ) ) {
    add_theme_support( 'custom-background', array( 'wp-head-callback' => 'change_custom_background_cb', 'default-color' => 'fff' ) );
}
else {
    add_custom_background('change_custom_background_cb');
}

endif;

#3


正是我一直在寻找的东西:http://wp.tutsplus.com/articles/tips-articles/modifying-custom-background-feature-for-any-html-element-you-want/

赞(0)
未经允许不得转载:srcmini » wordpress自定义背景主题支持

评论 抢沙发

评论前必须登录!