custom_register没有显示在前端

时间:2019-07-18 03:04:02

标签: php wordpress

我已经将此功能注册到主题定制中。但这并没有显示在前端(自定义部分)。

// Header section customize field
function header($wp_customize){

$wp_customize->add_section('header_section', array(
    'title' => 'Header Section'
));

$wp_customize->add_setting('header_headline', array(
    'default' => 'Hello, I\'m'
));

$wp_customize->add_control(new WP_Customize_control($wp_customize, 'header_headline_control', array(
    'label' => 'Headline',
    'section' => 'header_section',
    'setting' => 'header_headline'
)));

}
add_action('customize_register','header');

3 个答案:

答案 0 :(得分:1)

一个著名的例子在这里:

// Header section customize field
function alpha_customize($wp_customize)
{
    $wp_customize->add_setting('header_bg_color', array(
        'default' => "#4285f4",
        'transport' => 'refresh'
    ));

    $wp_customize->add_section('alpha_color_theme_section', array(
        'title' => __('Color', 'alpha'),
        'priority' => 30
    ));

    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'theme_color', array(
        'label' => __('Header Color', 'alpha'),
        'section' => 'alpha_color_theme_section',
        'settings' => 'header_bg_color'
    )));
}
add_action('customize_register', 'alpha_customize');

// Change and reflect value accordingly
function alpha_example_head()
{
    $header_color = get_theme_mod('header_bg_color', '#4285f4');
    $style = <<<EOD
    <style>
    .header h1.heading a, h3.tagline{
        color:{$header_color};
    }
    </style>
EOD;
    echo $style;
}

add_action('wp_head', 'alpha_example_head', 1100);

用于文本输入

// Header section customize field
function alpha_header($wp_customize)
{
    // Save settings to database
    $wp_customize->add_setting('alpha_header_headline', array(
        'default' => 'Hello, I\'m',
        'transport' => 'refresh'
    ));


    //Add a section for you settings in customizer option
    $wp_customize->add_section('alpha_header_section', array(
        'title' => __('Header title', 'alpha'),
        'priority' => 30 //the lowest the number the highest the input field will get preference
    ));

    $wp_customize->add_control(
        new WP_Customize_Control(
            $wp_customize,
            'header-text',
            array(
                'label'          => __( 'Header Title', 'alpha' ),
                'section'        => 'alpha_header_section',
                'settings'       => 'alpha_header_headline',
                'type'           => 'text'
            )
        )
    );
}
add_action('customize_register', 'alpha_header');

然后使用javascript更新您的文本值。也用您的主题名称替换alpha。完整的参考资料可以在这里找到:https://codex.wordpress.org/Theme_Customization_API

谢谢,让我知道这是否适合您。

答案 1 :(得分:0)

header_headline_controladd_control()中不正确。您需要保留header_headline。它应该与add_seting()中的相同。否则它将无法正常工作。

请参阅文档https://developer.wordpress.org/themes/customize-api/customizer-objects/

答案 2 :(得分:0)

我想添加图像以便使用

    // Provider Image
$wp_customize->add_setting('header-image');

$wp_customize->add_control(new WP_Customize_Cropped_Image_Control($wp_customize, 'header-image', array(
    'label' => 'Add Image',
    'section' => 'header-section',
    'settings' => 'header-image',
    'width' => 426,
    'height' => 642
)));

然后回声

<img src="<?php echo wp_get_attatchment_url(get_theme_mod('header-image')) ?>">

上传图像工作正常,但是页面只是在加载。