【问题标题】:Wordpress Customizer keeps hiding custom sectionsWordpress Customizer 不断隐藏自定义部分
【发布时间】:2021-02-28 12:56:48
【问题描述】:

我有添加定制器部分和选项的定制插件。 这些部分可以在定制器屏幕上短暂看到,但随后消失。这种行为发生在所有主题上(我在其他网站上使用我的插件)。

可能是因为主题中尚未使用设置字段,但即使我创建自己的主题(主要使用此插件)并在主题代码中的某处添加echo get_theme_mod('setting-key'),这些部分也是仍然被wordpress隐藏。

我有干净的 Wordpress 安装版本 5.2.2,使用默认的 29 主题,只有 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,并且这些部分消失了。

感谢任何帮助。

【问题讨论】:

  • 听起来好像有一些 javascript 隐藏了您的部分。您是否在主题或插件中执行任何自定义 js?先检查那里。如果您自己的 js 没有导致此问题,请一次停用所有其他插件,并检查每次停用后您的部分是否仍处于隐藏状态。
  • @kenef 感谢您的意见。是的,我安装了干净的 5.2.2,停用了所有插件(jQuery 更新除外),并且我检查了我的所有 JS 代码是否会导致此问题。而且什么都没发现。我也再次浏览了文档并稍微更新了代码。仍然没有修复,这里是 pastebin:pastebin.com/ScG7DTqa
  • 我有预感告诉我它是你的 jQuery 更新插件。您是否尝试过禁用它?
  • 我找到了@kenef,我只有在is_admin() 检查之后才初始化我的定制器选项。没有意识到重新加载前端预览后,不会为预览加载定制器选项,这反过来会影响定制器 UI。初始化is_admin() 之外的选项检查修复了它。谢谢你的时间:)
  • 万岁!我很高兴你明白了,伙计。

标签: php wordpress


【解决方案1】:

自定义部分保持隐藏的另一个原因是它们没有任何控件。当一个部分为空时,WordPress 默认隐藏它。

这是一个完整的工作代码,用于在定制器中添加一个带有一个控件的部分:

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' );

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多