2014-12-06 47 views
0

我很新的PHP,真的可以使用一些幫助大聲笑。WordPress的主題定製

我剛剛發現了Wordpress的主題定製器,我正試圖將其實現爲我創建的新主題。

我使用這種添加背景圖片選項: -

// Upload Custom Background 

$wp_customize->add_setting('simplistic_body_bg'); 

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'simplistic_body_bg', array(
'label' => __('Upload a custom background image', 'simplistic'), 
'default' => 'img/bright_squares.png', 
'section' => 'simplistic_background', 
'settings' => 'simplistic_body_bg', 
))); 

然後在header.php中

<style> 
     body { color: <?php echo $simplistic_body_color; ?>; background-image:url(<?php echo esc_url(get_theme_mod('simplistic_body_bg')); ?>);} 
    </style> 

它的工作,但是默認的備用圖片不能正常工作使用此作爲空的background-image:url()仍然存在。

我嘗試在定製器設置中添加「之前」和「之後」,但這不起作用。誰能幫我?

我基本上需要回退bg圖像選項來工作。

非常感謝:-)

繼...

我已經嘗試了修改建議,但他們似乎並沒有在所有輸出的默認數據。唯一可行的是:

<?php 
     $simplistic_body_color = get_option('simplistic_body_color'); 
     $simplistic_link_color = get_option('simplistic_link_color'); 
     $simplistic_hover_color = get_option('simplistic_hover_color'); 
     $simplistic_icon_color = get_option('simplistic_icon_color'); 
     $simplistic_footer_color = get_option('simplistic_footer_color'); 
     $simplistic_tag_color = get_option('simplistic_tag_color'); 
     $simplistic_widget_header_color = get_option('simplistic_widget_header_color'); 
    ?> 
    <style> 
     body { color: <?php echo $simplistic_body_color; ?>;} 
     a { color: <?php echo $simplistic_link_color; ?>; } 
     a:hover { color: <?php echo $simplistic_hover_color; ?>; } 
     .fa { color: <?php echo $simplistic_icon_color; ?>; } 
     .footer-wrapper { background-color: <?php echo $simplistic_footer_color; ?>; } 
     .tagcloud a { background-color: <?php echo $simplistic_tag_color; ?>; } 
     h3.widget-title { background-color: <?php echo $simplistic_widget_header_color; ?>; } 
    </style> 

    <?php if (get_theme_mod('simplistic_body_bg')) : ?> 

    <style> 
     body { background-image:url(<?php echo esc_url(get_theme_mod('simplistic_body_bg')); ?>);} 
    </style> 

    <?php else : ?> 

    <style> 
     body { background-image: url(<?php bloginfo('template_url'); ?>/img/bright_squares.png);} 
    </style> 

    <?php endif; ?> 

但它感覺不太乾淨。我不知道如何將'if else'放入一個樣式括號中。

+0

您還沒有爲'add_setting()'添加第二個參數[見Codex](http://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_setting#Parameters)。 – 2014-12-06 11:32:34

回答

0

由於user3734309你錯過add_setting的第二個參數:

$wp_customize->add_setting('simplistic_body_bg', array(
    'default' => 'img/bright_squares.png' 
)); 

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'simplistic_body_bg', array(
'label' => __('Upload a custom background image', 'simplistic'), 
'section' => 'simplistic_background', 
'settings' => 'simplistic_body_bg', 
))); 

不知道你有一個add_controldefault


更新: 下面是一個例子,我有我的主題,而這工作了顏色:

$wp_customize->add_setting('wp27331869_navbar_background', array(
    'default' => '#ffffff', 
    'transport' => 'postMessage' 
)); 
$wp_customize->add_control(
    new Pluto_Customize_Alpha_Color_Control(
     $wp_customize, 
     'wp27331869_navbar_background', 
     array(
      'label'  => 'Header background', 
      'section' => 'nav', 
      'settings' => 'wp27331869_navbar_background', 
      'priority' => 15 
     ) 
    ) 
); 

,並在前端:

echo 'background:' . get_theme_mod('wp27331869_navbar_background', '#ffffff');

也許你可以嘗試,並讓它工作然後根據你的需要調整它。

+0

感謝您的回覆。我已經嘗試過修改後的代碼,但它不能解決問題。它似乎根本不輸出默認數據。 – user3734309 2014-12-06 13:13:57

+0

我認爲'add_settings'中的默認值僅適用於後端(在定製主題時默認選擇)。如果您想爲前端設置默認值,請嘗試使用以下內容: 'get_theme_mod('simplistic_body_bg','img/bright_squares.png');' check [get_theme_mod](http://codex.wordpress .org/Function_Reference/get_theme_mod)上的wp codex – karimhossenbux 2014-12-06 13:23:16

+0

謝謝。我嘗試過,但它是一個正確的痛苦,並不會輸出默認的數據grr。唯一能做的就是這樣做: - – user3734309 2014-12-06 13:33:21