【问题标题】:Wordpress - customizer setting overwrites previous settingWordpress - 定制器设置覆盖以前的设置
【发布时间】:2018-08-16 09:36:27
【问题描述】:

我第一次使用 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 );
        }
    }

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    对于每个新设置,它必须有一个唯一的 ID,您已经为这两个设置使用了 custom_header_settings

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 2013-07-26
      • 2011-12-17
      • 2023-03-26
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多