【问题标题】:WordPress Customizer API not showing Panel/SectionWordPress Customizer API 不显示面板/部分
【发布时间】:2022-12-09 14:21:54
【问题描述】:

我是 Wordpress 主题开发的初学者,正在学习定制器 API。 但是,我被困在尝试使用 Customizer API 来使用 WordPress Customizer 更改导航菜单背景颜色的地方,但它没有显示任何内容,尽管我已经根据我的知识包含了所有必要的行。这是我试图在内部实现的代码 - functions.php:

function textdomain_pro_theme($wp_customize) {
$wp_customize->add_panel( 'pro_features', array(
    'title' => 'Pro Features',
    'priority' => 10 
));
$wp_customize->add_section( 'color_picking' , array(
    'title' => 'Color Settings',
    'panel' => 'pro_features',
    'priority' => 30
      ));

$wp_customize->add_setting( 'nav_menu_bgcolor', array(
    'type' => 'theme_mod',
    'capability' => 'edit_theme_options',
    'default' => '#ff2525',
    'transport' => 'refresh',
    'sanitize_callback' => 'sanitize_hex_color',
    ));
$wp_customize->add_control( 'nav_menu_bgcolor', array(
    'label' => 'Navigation Bar Color',
    'type' => 'color',
    'section' => 'pro_features',
    ));
}
add_action( 'customize_register', 'textdomain_pro_theme' );

CUSTOMIZER 不显示新部分。什么地方出了错?

我当前的 WORDPRESS 版本是 6.1

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    您在 add_control('nav_menu_bgcolor') 中使用了错误的部分 ID。替换为color_picking

    如果有帮助,请尝试此代码。

    function textdomain_pro_theme($wp_customize) {
    $wp_customize->add_panel( 'pro_features', array(
        'title' => 'Pro Features',
        'priority' => 10 
    ));
    $wp_customize->add_section( 'color_picking' , array(
        'title' => 'Color Settings',
        'panel' => 'pro_features',
        'priority' => 30
          ));
    
    $wp_customize->add_setting( 'nav_menu_bgcolor', array(
        'type' => 'theme_mod',
        'capability' => 'edit_theme_options',
        'default' => '#ff2525',
        'transport' => 'refresh',
        'sanitize_callback' => 'sanitize_hex_color',
        ));
    $wp_customize->add_control( 'nav_menu_bgcolor', array(
        'label' => 'Navigation Bar Color',
        'type' => 'color',
        'section' => 'color_picking',
        ));
    }
    add_action( 'customize_register', 'textdomain_pro_theme' );
    

    【讨论】:

    • 你说的对。谢谢你。另外,我又犯了一个错误。 'type' => 值应该是文本而不是颜色,
    • 我希望你解决了你的问题,然后将答案标记为已接受。
    【解决方案2】:

    在函数 PHP 文件中插入代码

    函数 textdomain_pro_theme($wp_customize) {

    $main_section_id = 'pro_features';
    $wp_customize->add_section( $main_section_id, array(
        'title' => 'Color Picking',
        'priority' => 30
    ));
    
    $wp_customize->add_setting( 'nav_menu_bgcolor', array(
        'type' => 'theme_mod',
        'capability' => 'edit_theme_options',
        'default' => '#ff2525',
        'transport' => 'refresh',
        'sanitize_callback' => 'sanitize_hex_color',
    ));
    
    $wp_customize->add_control( 'nav_menu_bgcolor', array(
        'label' => 'Navigation Bar Color',
        'type' => 'color',
        'section' => $main_section_id,
    ));
    }
    add_action( 'customize_register', 'textdomain_pro_theme' );
    
    // input the color in your Navigation Menu background color section
    echo get_theme_mod('nav_menu_bgcolor');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-11
      • 2019-11-07
      • 2018-03-28
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多