向主题定制器添加下拉菜单

时间:2017-06-14 作者:Farhad Sakhaei

如何向WordPress主题定制器添加下拉控件并在主题源中使用其值
我想使用下拉值更改一些CSS文件名

非常感谢你

1 个回复
最合适的回答,由SO网友:Farhad Sakhaei 整理而成

Add Section to Theme Customizer:

$wp_customize->add_section( \'parsmizban_options\', 
 array(
    \'title\'       => __( \'Theme Options\', \'parsmizban\' ), //Visible title of section
    \'priority\'    => 20, //Determines what order this appears in
    \'capability\'  => \'edit_theme_options\', //Capability needed to tweak
    \'description\' => __(\'Allows you to customize settings for Theme.\', \'parsmizban\'), //Descriptive tooltip
 ) 
);
Add new Setting:

$wp_customize->add_setting( \'bootstrap_theme_name\', //No need to use a SERIALIZED name, as `theme_mod` settings already live under one db record
 array(
    \'default\'    => \'default\', //Default setting/value to save
    \'type\'       => \'theme_mod\', //Is this an \'option\' or a \'theme_mod\'?
    \'capability\' => \'edit_theme_options\', //Optional. Special permissions for accessing this setting.
    //\'transport\'  => \'postMessage\', //What triggers a refresh of the setting? \'refresh\' or \'postMessage\' (instant)?
 ) 
);
Add a new control:

/*支持基本输入类型text, checkbox, textarea, radio, selectdropdown-pages.* 其他输入类型,如email, url, number, hiddendate 隐式支持*/

//3. Finally, we define the control itself (which links a setting to a section and renders the HTML controls)...
$wp_customize->add_control( new WP_Customize_Control(
 $wp_customize, //Pass the $wp_customize object (required)
 \'parsmizban_theme_name\', //Set a unique ID for the control
 array(
    \'label\'      => __( \'Select Theme Name\', \'parsmizban\' ), //Admin-visible name of the control
    \'description\' => __( \'Using this option you can change the theme colors\' ),
    \'settings\'   => \'bootstrap_theme_name\', //Which setting to load and manipulate (serialized is okay)
    \'priority\'   => 10, //Determines the order this control appears in for the specified section
    \'section\'    => \'parsmizban_options\', //ID of the section this control should render in (can be one of yours, or a WordPress default section)
    \'type\'    => \'select\',
    \'choices\' => array(
        \'default\' => \'Default\',
        \'cerulean\' => \'Cerulean\',
        \'cosmo\' => \'Cosmo\',
        \'cyborg\' => \'cyborg\',
    )
)
) );
Use this control:

<?php esc_html_e( get_theme_mod( \'bootstrap_theme_name\' ) ); ?>

结束