第一次使用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
对于每个新设置, 它必须具有唯一的ID, 并且你都使用了custom_header_settings。
参考:https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
评论前必须登录!
注册