自定义背景默认颜色不变

时间:2014-04-18 作者:Nathan Powell

我需要为主题的背景设置几个默认状态。其想法是,在选择颜色方案时,背景会发生变化。除了背景色之外,一切都在工作和变化。只有从管理屏幕保存时,颜色才会更改。我错过什么了吗?

add_action( \'after_setup_theme\', \'theme_custom_background\');

function theme_custom_background(){
  $theme = genesis_get_option(\'bf_style_selection\');

  switch ($theme) {
    case \'bf-blue\':
      $bg_color = \'def1fc\';
      $bg_image = \'header-bg-blue.gif\';
      break;
    case \'bf-black\':
      $bg_color = \'dadada\';
      $bg_image = \'header-bg-black.gif\';
      break;
  }

  $defaults = array(
    \'default-image\'          => get_stylesheet_directory_uri().\'/images/\'.$bg_image,
    \'default-color\'          => $bg_color,
    \'default-repeat\'         => \'repeat-x\',
    \'default-position-x\'     => \'top\',
    \'wp-head-callback\'       => \'_custom_background_cb\',
    \'admin-head-callback\'    => \'\',
    \'admin-preview-callback\' => \'\'
  );

  add_theme_support( \'custom-background\', $defaults);
}

1 个回复
最合适的回答,由SO网友:Nathan Powell 整理而成

我已经找出了这个问题。它是_custom_background_cb 中的函数wp-includes/theme.php 文件在第1272行,注释“必须在style.css中指定默认值。它将不会在此处打印。”

第1272行是检索颜色的位置:

$color = get_theme_mod( \'background_color\' );
如果我们将其更改为:

$color = get_theme_mod( \'background_color\', get_theme_support( \'custom-background\', \'default-color\' ) );
我们可以获得默认颜色,而无需将其保存在theme_mod. 我们仍然可以使用保存的值覆盖颜色。

因此,我将回调复制到自己的函数中,这就是有效的方法:

add_action( \'after_setup_theme\', \'theme_custom_background\');

function theme_custom_background(){
  $theme = genesis_get_option(\'bf_style_selection\');

  switch ($theme) {
    case \'bf-blue\':
      $bg_color = \'def1fc\';
      $bg_image = \'header-bg-blue.gif\';
      break;
    case \'bf-black\':
      $bg_color = \'dadada\';
      $bg_image = \'header-bg-black.gif\';
      break;
  }

  $defaults = array(
    \'default-image\'          => get_stylesheet_directory_uri().\'/images/\'.$bg_image,
    \'default-color\'          => $bg_color,
    \'default-repeat\'         => \'repeat-x\',
    \'default-position-x\'     => \'top\',
    \'wp-head-callback\'       => \'_bf_custom_background_cb\',
    \'admin-head-callback\'    => \'\',
    \'admin-preview-callback\' => \'\'
  );

  add_theme_support( \'custom-background\', $defaults);
}

function _bf_custom_background_cb() {
  // $background is the saved custom image, or the default image.
  $background = set_url_scheme( get_background_image() );

  // $color is the saved custom color.
  // Added second parameter to get_theme_mod() to get_theme_support() as we need default colors to show with needing to save.
  $color = get_theme_mod( \'background_color\', get_theme_support( \'custom-background\', \'default-color\' ) );

  if ( ! $background && ! $color )
    return;

  $style = $color ? "background-color: #$color;" : \'\';

  if ( $background ) {
    $image = " background-image: url(\'$background\');";

    $repeat = get_theme_mod( \'background_repeat\', get_theme_support( \'custom-background\', \'default-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\', get_theme_support( \'custom-background\', \'default-position-x\' ) );
    if ( ! in_array( $position, array( \'center\', \'right\', \'left\' ) ) )
      $position = \'left\';
    $position = " background-position: top $position;";

    $attachment = get_theme_mod( \'background_attachment\', get_theme_support( \'custom-background\', \'default-attachment\' ) );
    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">
body.custom-background { <?php echo trim( $style ); ?> }
</style>
<?php
}

结束

相关推荐