2013-03-23 73 views
1

我使用WP中的默認主題的兒童主題,我已經改變了背景圖片(在wp-admin)...它是用wp_head()生成的;在header.php中功能...輸出源代碼如下所示:WordPress的自定義背景主題支持

<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文件或其他任何東西),因爲某些原因,但我不能找到如何做到這一點。我已經搜查,沒有resault :(

任何人都知道,該怎麼辦呢(當然,不修改WP核心文件 - 只是主題修改或插件)所有主題文件

回答

1

在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

說實話,我不是一個球迷。的WordPress將不可調整的功能添加到y主題。這是我解決它的方式。我從body標記中刪除了custom-background類。希望對某人仍然有用。

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標籤。現在您可以決定如何實現背景圖像。