我正在尝试为我的客户创建可定制的主题。目标是使他们能够在主题定制器中修改某些样式。
我不想在html代码中插入customizer的变量,而是创建了一个css-php 文件除了更改的实时预览不能实时反映这些更改外,其他一切都很正常。相反,每次更改后,我都必须刷新页面。我个人并不感到烦恼,但我的客户却感到烦恼。
你对此有何想法?
这就是我的functions.php:
function im_customize_register( $wp_customize ) {
$colors = array();
$colors[] = array(
  \'slug\'=>\'im_header_bcolor\', 
  \'default\' => \'#556d80\',
  \'label\' => __(\'Header Background Color\', \'impressive\')
);
$colors[] = array(
  \'slug\'=>\'im_nav_bar_bcolor\', 
  \'default\' => \'#434044\',
  \'label\' => __(\'Navigation Bar Background Color\', \'impressive\')
);
foreach( $colors as $color ) {
  // SETTINGS
  $wp_customize->add_setting(
    $color[\'slug\'], array(
      \'default\' => $color[\'default\'],
      \'type\' => \'theme_mod\', 
      \'capability\' => 
      \'edit_theme_options\'
    )
  );
  // CONTROLS
  $wp_customize->add_control(
    new WP_Customize_Color_Control(
      $wp_customize,
      $color[\'slug\'], 
      array(\'label\' => $color[\'label\'], 
      \'section\' => \'colors\',
      \'settings\' => $color[\'slug\'])
    )
  );
}
}
add_action( \'customize_register\', \'im_customize_register\' );
function im_theme_styles() {
wp_enqueue_style( \'main_css\', get_template_directory_uri() . \'/style.css\' );
}
add_action( \'wp_enqueue_scripts\', \'im_theme_styles\' );
$custom_css= \'
  .im_header { background-color: \' . get_theme_mod(\'im_header_bcolor\') . \'; }
  .im_nav_bar { background-color: \' . get_theme_mod(\'im_nav_bar_bcolor\') . \'; }
  \';
wp_add_inline_style (\'main-style\', $custom_css);
 还有我的
css.php:
<?php header("Content-type: text/css; charset: UTF-8"); ?>
<?php
  $parse_uri = explode( \'wp-content\', $_SERVER[\'SCRIPT_FILENAME\'] );
  require_once( $parse_uri[0] . \'wp-load.php\' );
  $im_header_bcolor = get_theme_mod(\'im_header_bcolor\');
  $im_nav_bar_bcolor = get_theme_mod(\'im_nav_bar_bcolor\');
?>
  .im_header { background-color:  <?php echo $im_header_bcolor; ?>; }
  .im_nav_bar { background-color:  <?php echo $im_nav_bar_bcolor; ?>; }