問題描述
如何添加説明 $wp_customize->add_control?我發現我真的需要在一些控件上添加一個簡短的描述,但它看起來不太可能。
我注意到您可以向 $wp_customize->add_section 添加説明,但這只是一個工具提示。
這是理想的我想做的,但不確定如何輸出它,如果這是可能的:
$wp_customize->add_control( 'theme_options[some_option_name]', array(
'label' => 'This Is Some Option',
'section' => 'theme_name_section',
'type' => 'text',
'description' => 'Wish this existed', // this isn't possible
));
最佳解決方案
這是通過擴展要使用的控件來實現的一種方法。
下面是一個例子,我們擴展了文本控件,並添加了一個額外的描述,如屏幕截圖所示:
function mytheme_customizer( $wp_customize ) {
class Custom_Text_Control extends WP_Customize_Control {
public $type = 'customtext';
public $extra = ''; // we add this for the extra description
public function render_content() {
?>
<label>
<span><?php echo esc_html( $this->extra ); ?></span>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
</label>
<?php
}
}
$wp_customize->add_section('customtext_section', array(
'title'=>__('My Custom Text','mytheme'),
)
);
$wp_customize->add_setting('mytheme_options[customtext]', array(
'default' => '',
'type' => 'customtext_control',
'capability' => 'edit_theme_options',
'transport' => 'refresh',
)
);
$wp_customize->add_control( new Custom_Text_Control( $wp_customize, 'customtext_control', array(
'label' => 'My custom Text Setting',
'section' => 'customtext_section',
'settings' => 'mytheme_options[customtext]',
'extra' =>'Here is my extra description text ...'
) )
);
}
add_action( 'customize_register', 'mytheme_customizer' ,10,1);
檢查 WP_Customize_Control 類的源是很有幫助的:
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-customize-control.php
希望這可以幫助。
次佳解決方案
對於任何在 WordPress 4.0 發佈後遇到此問題的人,不再需要自定義控件。此功能可以直接進入 WordPress:https://core.trac.wordpress.org/ticket/27981 。
第三種解決方案
description 參數添加了控件下的描述。如果要添加控件標題以上的東西,如額外的標題或其他東西,可以使用 customize_render_control_{id}操作。例如,如果要在控件上方添加一個帶有 hi_shawn 的按鈕,您可以執行以下操作:
add_action( 'customize_render_control_hi_shawn', function(){
printf( '<a href="%s">%s</a>', 'http://hiroy.club', __( 'Hi Shawn', 'text-domain' ) );
});
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
