Wordpress主题定制器

时间:2014-12-06 11:18:55

标签: php wordpress

我是非常新的php,可以真正使用一些帮助大声笑。

我刚刚发现了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>

它确实有效,但是默认的后备图像不能用作空白背景图像:url()仍然存在。

我尝试在&#39;之前添加&#39;和&#39;之后&#39;到自定义程序设置但这不起作用。任何人都可以帮助我吗?

我基本上需要使用fallback bg image选项来工作。

非常感谢: - )

继续......

我已尝试过建议的修改,但他们似乎根本没有输出默认数据。唯一可行的是:

<?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; ?>

但它感觉不是很干净。我不知道怎么把&#39; if else else&#39;只有一个样式括号。

1 个答案:

答案 0 :(得分: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',
) ) );

不确定default中是否有add_control值。


<强>更新 这是我在我的主题中的一个例子,它正在为一种颜色工作:

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

也许你可以试试并让它适应你的需要。

相关问题