我编写了以下循环来更快地创建WordPress自定义控件:
<?php
/* Creates "Color Scheme" section */
$wp_customize->add_panel('color_scheme', array(
'title' => 'Color Scheme',
'priority' => 120,
));
/* Creates "Navbar" section */
$wp_customize->add_section('navbar', array(
'title' => 'Navbar',
'description' => '',
'priority' => 120,
'panel' => 'color_scheme'
));
/* Navbar Color Controls */
$color_controls = array("menu-bg-color", "menu-button-color", "menu-site-title-color", "menu-overlay-bg-color", "menu-items-color");
$color_labels = array("Background Color", "Button Color", "Title Color", "Overlay Background Color", "Overlay Item Color");
foreach($color_controls as $control) {
foreach($color_labels as $label) {
$wp_customize->add_setting($control);
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $control, array(
'label' => $label,
'section' => 'navbar',
'settings' => $control
)));
}
}
?>
这是结果:Screenshot of my WordPress customizer 它不是通过标签循环而只是使用最后一个值。
答案 0 :(得分:0)
$color_controls = array(
"menu-bg-color" => "Background Color",
"menu-button-color" => "Button Color",
"menu-site-title-color" => "Title Color",
"menu-overlay-bg-color" => "Overlay Background Color",
"menu-items-color" => "Overlay Item Color"
);
foreach ($color_controls as $control => $label) {
$wp_customize->add_setting($control);
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $control, array(
'label' => $label,
'section' => 'navbar',
'settings' => $control
)));
}
我认为这就是你要找的东西。当您想要array1中每个项目中的每个项目中的一个时,嵌套循环适用于array1中的每个项目。实际上,您需要一个键和值的关联数组,并循环遍历它们,其中键是您的控件,映射到您的值作为标签。