在安装新Theme主题之后,通过控制面板的customize功能,可以进入自定义界面,这时候可以看到很多选项,如何增加这类选项呢?


首先,在主题的inc/customizer.php中找到customize_register对应的函数,增加如下内容:

    $wp_customize->add_setting(

        'blog_disp',

        array(

            'sanitize_callback' => 'astrid_sanitize_checkbox',

        )    

    );  

    $wp_customize->add_control(

        'blog_disp',

        array(

            'type' => 'checkbox',

            'label' => __('Display post?', 'astrid'),

            'section' => 'blog_options',

            'priority' => 23

        )   

    );  

这段代码中新建了一个checkbox项,其method回调函数为astrid_sanitize_checkbox:

function astrid_sanitize_checkbox( $input ) {                                                                                                                                        

    if ( $input == 1 ) { 

        return 1;

    } else {

        return ''; 

    }   

}

在需要使用该选项的地方,添加如下代码:

<?php if ( get_theme_mod( 'blog_disp' ) == 1 ) : ?>

[需要被该checkbox控制的部分]

<?php endif;?>