2016-11-23 76 views
2

我使用下面的代碼一些CSS添加到頁面的頭從WordPress定製設置:WordPress的定製CSS的背景圖片

public static function header_output() { 
    ?> 
    <!--Customizer CSS--> 
    <style type="text/css"> 
     <?php self::generate_css('#site-title a', 'color', 'header_textcolor', '#'); ?> 
     <?php self::generate_css('body', 'background-color', 'background_color', '#'); ?> 
     <?php self::generate_css('a', 'color', 'link_textcolor'); ?> 
     <?php self::generate_css('#wrapper-1', 'background-color', 'section_1_background_color'); ?> 
     <?php self::generate_css('#wrapper-1', 'background-image', 'section_1_background_image'); ?> 
    </style> 
    <!--/Customizer CSS--> 
    <?php 
} 

,一切工作正常,除了背景圖像,因爲它輸出:

#wrapper-1 { background-image:filename.jpg; } 

代替:

#wrapper-1 { background-image: url("filename.jpg"); } 

有誰知道正確的方法修改下面的php行以在圖像周圍包含url(「」)?

<?php self::generate_css('#wrapper-1', 'background-image', 'section_1_background_image'); ?> 
+0

你有一個自定義的主題定製類,包含generate_css功能?它看起來像默認情況下只包含一個sprintf調用來形成css,這意味着如果樣式是「-image」樣式,它將不得不擴展爲包含url()語法。 – WEBjuju

回答

2

引用https://codex.wordpress.org/Theme_Customization_API#Sample_Theme_Customization_Class ...

展開了generate_css功能類似這樣的主題定製類:

/** 
* This will generate a line of CSS for use in header output. If the setting 
* ($mod_name) has no defined value, the CSS will not be output. 
* 
* @uses get_theme_mod() 
* @param string $selector CSS selector 
* @param string $style The name of the CSS *property* to modify 
* @param string $mod_name The name of the 'theme_mod' option to fetch 
* @param string $prefix Optional. Anything that needs to be output before the CSS property 
* @param string $postfix Optional. Anything that needs to be output after the CSS property 
* @param bool $echo Optional. Whether to print directly to the page (default: true). 
* @return string Returns a single line of CSS with selectors and a property. 
* @since MyTheme 1.0 
*/ 
public static function generate_css($selector, $style, $mod_name, $prefix='', $postfix='', $echo=true) { 
    $return = ''; 
    $mod = get_theme_mod($mod_name); 


    // fix the issue here: 
    if ($style=='background-image' && !empty($mod)) { 
    $mod = 'url("'.$mod.'")'; 
    } 


    if (! empty($mod)) { 
    $return = sprintf('%s { %s:%s; }', 
     $selector, 
     $style, 
     $prefix.$mod.$postfix 
    ); 
    if ($echo) { 
     echo $return; 
    } 
    } 
    return $return; 
} 
+0

這工作很好。非常感謝@WEBjuju! –

+0

這是個好消息!你應該對這個代碼存在的任何回購提交一個錯誤修復 - 你也應該記下這個功能) – WEBjuju

+0

我從你引用的頁面獲得了代碼,並向它添加了背景圖片,但至少如果其他人碰到相同的代碼問題他們應該能夠輕鬆找到這篇文章;) –