Wordpress-定制程序设置会覆盖以前的设置

时间:2018-08-16 09:36:27

标签: wordpress wordpress-theming

我第一次使用wordpress时,在尝试在“自定义程序”面板中添加新选项时遇到了一些困难。

我设法获得了一个设置,但是我的第二个设置将覆盖第一个设置,结果是在定制器面板中仅显示一个选项。

我不明白,在这里看不到我在做错什么,也无法在互联网上找到任何答案,因此,也许有人在这里可以帮助我。 :)

这是我目前的代码:

    <?php
    add_action( 'customize_register', 'twentyseventeenchild_register_theme_customizer' );
    /*
    * Register Our Customizer Stuff Here
    */
    function twentyseventeenchild_register_theme_customizer( $wp_customize ) {
        // Create custom panel.
        $wp_customize->add_panel( 'child_custom_settings', array(
            'priority'       => 500,
            'theme_supports' => '',
            'title'          => __( 'Custom settings', 'twentyseventeen-child' ),
            'description'    => __( 'Set editable text for certain content.', 'twentyseventeen-child' ),
        ) );

        // Add section.
        $wp_customize->add_section( 'custom_header_settings' , array(
            'title'    => __('Header', 'twentyseventeen-child'),
            'panel'    => 'child_custom_settings',
            'priority' => 10
        ) );

        //  =============================
        //  = Header title              =
        //  =============================
        $wp_customize->add_setting( 'header_options[title]', array(
            'default'           => __( '', 'twentyseventeen-child' ),
            'sanitize_callback' => 'sanitize_text'
        ) );

        $wp_customize->add_control( new WP_Customize_Control(
            $wp_customize,
            'custom_header_settings',
                array(
                        'label'    => __( 'Header title', 'twentyseventeen-child' ),
                        'section'  => 'custom_header_settings',
                        'settings' => 'header_options[title]',
                        'type'     => 'text'
                )
            )
        );

        //  =============================
        //  = Header text               =
        //  =============================
        $wp_customize->add_setting( 'header_options[text]', array(
            'default'           => __( '', 'twentyseventeen-child' ),
        ) );

        $wp_customize->add_control( new WP_Customize_Control(
            $wp_customize,
            'custom_header_settings',
                array(
                        'label'    => __( 'Header text', 'twentyseventeen-child' ),
                        'section'  => 'custom_header_settings',
                        'settings' => 'header_options[text]',
                        'type'     => 'text'
                )
            )
        );

        // Sanitize text
        function sanitize_text( $text ) {
                return sanitize_text_field( $text );
        }
    }

1 个答案:

答案 0 :(得分:2)

对于每个新设置,它必须具有唯一的 ID ,并且您对这两个设置均使用了 custom_header_settings

参考:https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control