我需要在此foreach中在我的主题定制屏幕上构建一个select:
foreach($wp_registered_sidebars as $sidebar_id => $sidebar)
{
if($sidebar_id == $val)
$sidebar_id.$sidebar[\'name\']
}
以下是自定义侧栏选择的设置和控制代码。
动态列表需要放在“选项”=>数组()中;
这可能吗?
// Add Layout setting
$wp_customize->add_setting(
// ID
\'sidebar_left_selection\',
// Arguments array
array(
\'default\' => \'none\',
\'type\' => \'option\',
)
);
// Add Layout control
$wp_customize->add_control(
// ID
\'sidebar_left_selection\',
// Arguments array
array(
\'type\' => \'select\',
\'label\' => __( \'Sidebar Left Selection\', \'webcodexcustomizer\' ),
\'section\' => \'layout_section\',
\'choices\' => array(
\'none\' => __( \'None\', \'webcodexcustomizer\' ),
\'sidebar_test\' => __( \'Test Sidebar\', \'webcodexcustomizer\' ),
),
\'priority\' => 36
)
);
SO网友:CodeX
解决了这个问题,您必须创建一个扩展WP\\u Customize\\u控件的新类:
// Add Layout setting
$wp_customize->add_setting(
// ID
\'sidebar_left_selection\',
// Arguments array
array(
\'default\' => \'none\',
\'sanitize_callback\' => \'webcodexcustomizer_sanitize_sidebar_selection\'
)
);
class customize_sidebar_selection extends WP_Customize_Control {
public function render_content()
{
global $wp_registered_sidebars;
// The actual fields for data entry
$output = "<span>" . esc_html( $this->label ) . "</span>";
$output .= "<select name=\'custom_sidebar_left\'>";
// Add a default option
$output .= "<option";
if($val == "default")
$output .= " selected=\'selected\'";
$output .= " value=\'default\'>".__(\'default\', $themename)."</option>";
// Fill the select element with all registered sidebars
foreach($wp_registered_sidebars as $sidebar_id => $sidebar)
{
$output .= "<option";
if($sidebar_id == $val)
$output .= " selected=\'selected\'";
$output .= " value=\'".$sidebar_id."\'>".$sidebar[\'name\']."</option>";
}
$output .= "</select>";
echo $output;
}
}
// Add Layout control
$wp_customize->add_control(
new customize_sidebar_selection(
$wp_customize, \'sidebar_left_selection\', array(
\'label\' => \'Sidebar Left Selection\',
\'section\' => \'layout_section\',
\'priority\' => 36,
)
)
);