【问题标题】:WordPress Theme Customizer Content Not Displaying on PageWordPress 主题定制器内容未显示在页面上
【发布时间】:2014-11-02 04:59:36
【问题描述】:

全部

我正在使用为网站开发的自定义 WordPress 主题。我在页脚左侧有一个用于邮寄地址的字段,我希望它可以通过 WordPress 中的主题定制器进行编辑。我已将相关字段添加到定制器中,但该字段的内容未显示在页面上。

在functions.php中,以下代码将字段添加到主题定制器:

//Adds footer address to theme customizer   

function sanitize_footer_address($input){
    return strip_tags(stripslashes($input));    
}

function portfolio_customize_register($wp_customize){

    $wp_customize->add_section(
        'Footer', 
        array(
            'title' => __('Left Footer Content', 'portfolio'),
            'priority' => 200
        )
    );

    $wp_customize->add_setting(
        'footer_address', 
        array(
            'default' => '100 Main Street | Anytown, USA',
            'sanitize_callback' => 'sanitize_footer_address'
        )
    );

    $wp_customize->add_control( new WP_Customize_Control(
    $wp_customize, 
    'footer_address',
    array(
        'label' => 'Address to appear on left side of footer', 
        'section' => 'Footer', 
        'settings' => 'footer_address_text', 
        'type' => 'text'
    )
    )
 ); 
}

add_action(customize_register, portfolio_customize_register);

在footer.php模板中,相关标记为:

<p class="col md-4 text-muted" id="footer_address"><?php echo get_theme_mod('footer_address_text'); ?></p>

该字段的内容不显示在页面上。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    add_setting() 注册了错误的 ID,add_control() 在添加默认字段时不需要new WP_Customize_Control($wp_customize, ),只有在创建自定义WP_Customize_* 类时才需要。

    $wp_customize->add_setting(
        'footer_address_text', 
        array(
            'default' => '100 Main Street | Anytown, USA',
            'sanitize_callback' => 'sanitize_footer_address'
        )
    );
    
    $wp_customize->add_control(
        'footer_address',
        array(
            'label' => 'Address to appear on left side of footer', 
            'section' => 'Footer', 
            'settings' => 'footer_address_text', 
            'type' => 'text'
        )
     ); 
    

    【讨论】:

    • 谢谢,@brasofilo。它仍然需要新的 WP_Customize_Control 声明,因为这不是默认字段,但更正您指出的 ID 错误可以解决问题。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多