WordPress:如何在Customizer中使用活动回调

时间:2017-10-30 13:20:35

标签: php wordpress wordpress-theming themes

在我的主题中,我使用Theme Customization API设置了两个选项,下面是代码片段。

我想在radio为真,checkbox为假,checkbox隐藏时显示radio选项。我尝试使用active_callback,但没有工作。那么,如何实现这个功能呢?

谢谢!

    // Related Post.
    $wp_customize->add_setting('_related_post', array(
        'capability' => 'edit_theme_options',
        'default' => 0,
        'transport' => 'postMessage',
    ));

    $wp_customize->add_control('_related_post', array(
        'settings'      => '_related_post',
        'label'         => __('Display Related Posts', 'typenow'),
        'section'       => '_theme_options',
        'type'          => 'checkbox',
        'priority'      => 30,
    ));
    
    // Related Post Num.
    
        $wp_customize->add_setting('_related_post_num', array(
        'capability' => 'edit_theme_options',
        'default' => '2',
        'transport' => 'postMessage',
    ));

    $wp_customize->add_control('_related_post_num', array(
        'settings'      => '_related_post_num',
        'label'         => __('Related Posts Number', 'typenow'),
        'section'       => '_theme_options',
        'type'          => 'radio',
        'priority'      => 35,
        'choices'       => array (
                            '2'  => __('Two posts', 'typenow'),
                            '4' => __('Four posts', 'typenow'),
                            ),
    ));

1 个答案:

答案 0 :(得分:0)

解决方案:

$wp_customize->add_control('_related_post_num', array(
    'settings'      => '_related_post_num',
    'label'         => __('Related Posts Number', 'typenow'),
    'section'       => '_theme_options',
    'type'          => 'radio',
    'priority'      => 35,
    'choices'       => array (
                        '2'  => __('Two posts', 'typenow'),
                        '4' => __('Four posts', 'typenow'),
                        ),
    'active_callback' => function(){
        return get_theme_mod( '_related_post', false );
    },
));