【问题标题】:Wordpress Theme CustomizerWordPress 主题定制器
【发布时间】:2014-12-06 11:18:55
【问题描述】:

我是 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>

它确实有效,但是默认的后备图像不起作用,因为空的 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' 放入一个样式括号中。

【问题讨论】:

  • 你还没有为add_setting()添加第二个参数.See Codex

标签: php wordpress


【解决方案1】:

作为 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_control 中是否有default 值。


更新: 这是我的主题中的一个示例,它适用于颜色:

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

也许您可以尝试一下,让它发挥作用,然后根据您的需要进行调整。

【讨论】:

  • 感谢您的回复。我已经尝试了修改后的代码,但不幸的是它并没有解决问题。它似乎根本没有输出默认数据。
  • 我认为add_settings 中的默认值仅适用于后端(自定义主题时默认选择)。如果您想要前端的默认值,请尝试使用以下内容:get_theme_mod('simplistic_body_bg', 'img/bright_squares.png'); 检查 wp 代码上的 get_theme_mod
  • 谢谢。我也试过了,但这很痛苦,不会输出默认数据grr。唯一有效的就是这样做:-
  • 用我在主题中使用的工作示例更新了我的第一个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 2014-12-25
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多