【问题标题】:wordpress customizer default settings not workingwordpress 定制器默认设置不起作用
【发布时间】:2015-10-05 19:24:05
【问题描述】:

我正在开发我的第一个 wordpress 主题,但遇到了一些问题。 我正忙着为主题构建一个定制器页面。 在定制器不会将默认设置解析为 css 之前,它一直很好。 我已经尝试重新构建函数文件,但没有任何效果。

你们可以看看代码,如果有什么问题告诉我,或者如果其他人有这个问题,请告诉我解决方案是什么。

这是我的 funtions.php 文件的代码

<?php

add_theme_support( 'menus' );
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
    array(
    'header-menu' => 'main'
    )
);
}
add_action( 'init', 'register_my_menus' );

add_theme_support( 'post-thumbnails' );

function claboxico_customize_register( $wp_customize ) {


//fonts array
$googlefonts = array(
        'arial'=>'Arial',
        'verdana'=>'Verdana, Geneva',
        'trebuchet'=>'Trebuchet',
        'trebuchet ms'=>'Trebuchet MS',
        //this list contains way more fonts
      );

//general panel
$wp_customize->add_panel( 'colors', array(
'title' => __( 'Colors' ),
'description' => 'Edit the general styling',
) );

//general colors section
$wp_customize->add_section('primairy_color', array(
  'title' => __('Primairy Colors', 'claboxico'),
  'panel' => 'colors',
  'description' => 'Edit the primairy color of the theme.'
));
//primairy color setting
$wp_customize->add_setting('primairy_color', array(
'default' => '#43bfd8',
));
//primairy color control
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'primairy_color', array(
'label' => __('Primairy Color', 'claboxico'),
'section' => 'primairy_color',
'setting' => 'primairy_color',
) ));


//link kleur
$wp_customize->add_section('link_color', array(
'title' => __('Link Colors', 'claboxico'),
'panel' => 'colors',
'description' => 'Edit the color of the links.'
));
$wp_customize->add_setting('link_color', array(
'default' => '#0eb1ed',
));
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'link_color', array(
'label' => __('Link Color', 'claboxico'),
'section' => 'link_color',
'setting' => 'link_color',
) ));

//plain text color
$wp_customize->add_section('paragraph_color', array(
'title' => __('Paragraph Colors', 'claboxico'),
'panel' => 'colors',
'description' => 'Edit the paragraph text color.'
));
$wp_customize->add_setting('paragraph_color', array(
'default' => '#000',
));
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'paragraph_color', array(
'label' => __('Link Color', 'claboxico'),
'section' => 'paragraph_color',
'setting' => 'paragraph_color',
) ));




//fonts panel
$wp_customize->add_panel( 'fonts', array(
  'title' => __( 'Fonts' ),
  'description' => 'Edit the fonts',
) );

  //heading fonts section
  $wp_customize->add_section('heading_font', array(
    'title' => __('Heading fonts', 'claboxico'),
    'panel' => 'fonts',
    'description' => 'Edit the font for h1, h2 etc.'
  ));
  $wp_customize->add_setting('heading_font', array(
    'default' => 'Rokkitt',
  ));

  $wp_customize->add_control( 'heading_font',array(
    'type' => 'select',
    'label' => __('Heading font', 'claboxico'),
    'section' => 'heading_font',
    'setting' => '',
    'choices' => $googlefonts,
    )
  );



}

function claboxico_css_customizer() {
?>

<style type="text/css">

  @import url(http://fonts.googleapis.com/css?family=<?php
    $font_sizes = ':300,400,700,900|';
    $heading_google_font = get_theme_mod('heading_font');
    $heading_font = str_replace(' ', '+', $heading_google_font);
    echo $heading_font;
    echo $font_sizes;

    $paragraph_google_font = get_theme_mod('paragraph_font');
    $paragraph_font = str_replace(' ', '+', $paragraph_google_font);
    echo $paragraph_font;
    echo $font_sizes;


   ?>);
  body{font-family: '<?php echo get_theme_mod('paragraph_font'); ?>'; color: <?php echo get_theme_mod('paragraph_color'); ?> }
  /* background color */
  .header{background-color: <?php echo get_theme_mod('header_background_color'); ?> ;}
  /* heading colors and font */
  h1, h2, h3, header .logo{color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ; font-family:'<?php    echo get_theme_mod('heading_font');     ?>';}
  /*link colors*/
  a, a:hover{color: <?php echo get_theme_mod('link_color');?>;}
  /* primairy colors */
  header{border-bottom-color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}
  header .mobile-header-icon, header nav ul li a{color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}

  footer{border-top-color: <?php echo get_theme_mod('primairy_color', '#43bfd8');?> ;}

</style>

<?php
}
add_action('wp_head', 'claboxico_css_customizer');
add_action( 'customize_register', 'claboxico_customize_register' );
?>

希望有人能帮我解决这个问题。

【问题讨论】:

    标签: php css wordpress settings default


    【解决方案1】:

    函数get_theme_mod可以带两个参数。

    • $name(必需)= 设置名称
    • $default(可选)=如果数据库中没有设置值,则为默认值

    查看 Codex 页面:https://codex.wordpress.org/Function_Reference/get_theme_mod

    示例:

    $color = get_theme_mod( 'link_color', '#ff0000' );
    

    在这个例子中,默认值#ff0000 被传递。如果设置了值,则保存的值将返回,否则$color 将获得默认值#ff0000

    【讨论】:

    • 我在 get_theme_mod 中使用了默认值,您可以在向下滚动时看到样式。这似乎也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多