我有添加定制化部分和选项的自定义插件。这些部分可以在定制器屏幕上短暂地看到,但随后会消失。这种行为在所有主题上都会发生(我在其他网站上使用我的插件)。
也许是因为主题中还没有使用设置字段,但即使我创建了自己的主题(这个插件主要用于这个主题),并在主题代码中的某个地方添加了echo get_theme_mod('setting-key'),这些部分仍然会被wordpress隐藏。
我有干净的Wordpress安装版本5.2.2,使用默认的二十九主题,只有jQuery更新插件活动。我已经检查了所有JS代码的潜在错误和任何隐藏发生的情况,但我这方面没有任何东西可以导致这一点。
这就是我在customize_register钩子中添加部分的方式:
add_action('customize_register', 'setup_section');
function setup_section($wp_customize){
// Add section
$wp_customize->add_section('section_id', array(
'title' => 'Section Title',
'priority' => 160,
));
// Add settings for a field
$wp_customize->add_setting('setting_id', array(
'default' => '',
'transport' => 'refresh',
));
// Add the field into a section and assign setting id
$wp_customize->add_control('setting_id', array(
'label' => 'Option Label',
'section' => 'section_id',
'settings' => 'setting_id',
'type' => 'text',
));
}PHP代码正常工作,但是在页面加载之后,我的所有自定义部分都添加了display: none;内联css,并且这些部分消失了。
任何帮助都是非常感谢的。
发布于 2021-02-28 12:56:48
自定义部分保持隐藏的另一个原因是当它们没有任何控件时。当某个部分为空时,默认情况下WordPress会将其隐藏。
下面是用一个控件在Customizer中添加一个部分的完整工作代码:
function mytheme_customize_register( $wp_customize ) {
$wp_customize->add_section('footer_settings_section', array(
'title' => 'Footer Text Section'
));
$wp_customize->add_setting('text_setting', array(
'default' => 'Default Text For Footer Section',
));
$wp_customize->add_control('text_setting', array(
'label' => 'Footer Text Here',
'section' => 'footer_settings_section',
'type' => 'textarea',
));
}
add_action( 'customize_register', 'mytheme_customize_register' );https://stackoverflow.com/questions/57611426
复制相似问题