Wordpress中使用的PHP foreach循环wp_customize->;添加\u控件

Wordpress中使用的PHP foreach循环wp_customize->;添加\u控件,php,wordpress,foreach,Php,Wordpress,Foreach,我正试图在WordPress定制程序的下拉列表中列出要使用的Google字体选择,但我很难正确执行此循环: $i = 0; foreach ($items as $font_value => $item) { $i++; $str = $item['family']; } 上面的字符串需要在下面的数组中才能生成选项列表: $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'oun

我正试图在WordPress定制程序的下拉列表中列出要使用的Google字体选择,但我很难正确执行此循环:

$i = 0;
foreach ($items as $font_value => $item) {
    $i++;
    $str = $item['family'];
}
上面的字符串需要在下面的数组中才能生成选项列表:

$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'ounox-fonts-display-control', array(
    'label' => 'Fonts Section',
    'section' => 'ounox-fonts-section',
    'settings' => 'ounox-fonts-display',
    'type' => 'select',
    'choices' => $str
)));
循环不需要计数器,因此
$i
是冗余的。 您还将在每次迭代中覆盖
$str

$str = '';
foreach ($items as $font_value => $item) {
    $str .= $item['family']; // . '##' in case you need a delimiter
}

选项
参数,而不是字符串,因此您需要将每个
$item['family']
保存到数组中,然后将该数组添加到参数中

Hapstyx还正确地指出,迭代循环不需要
$i++

选项
所需的下拉选项数组应如下所示:

$choices = array(
    'option-value-1'    => 'Option Title 1',
    'option-value-2'    => 'Option Title 2',
    'option-value-3'    => 'Option Title 3'
);
//predefine a blank array which we will fill with your options
$choices = array();

//loop through your items and that the values to the $choices array
foreach ($items as $font_value => $item) {
    $choices[$item['slug']] = $item['family']; //I'm assuming your $item array contains some sort of slug to set as the value, otherwise, comment the above out, and uncomment the below:
    // $choices[$item['family']] = $item['family']
}

//set your arguments
$args = array(
    'label'     => 'Fonts Section',
    'section'   => 'ounox-fonts-section',
    'settings'  => 'ounox-fonts-display',
    'type'      => 'select',
    'choices'   => $choices
);

//add the control
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'ounox-fonts-display-control', $args));
我们可以像这样构建这种类型的阵列:

$choices = array(
    'option-value-1'    => 'Option Title 1',
    'option-value-2'    => 'Option Title 2',
    'option-value-3'    => 'Option Title 3'
);
//predefine a blank array which we will fill with your options
$choices = array();

//loop through your items and that the values to the $choices array
foreach ($items as $font_value => $item) {
    $choices[$item['slug']] = $item['family']; //I'm assuming your $item array contains some sort of slug to set as the value, otherwise, comment the above out, and uncomment the below:
    // $choices[$item['family']] = $item['family']
}

//set your arguments
$args = array(
    'label'     => 'Fonts Section',
    'section'   => 'ounox-fonts-section',
    'settings'  => 'ounox-fonts-display',
    'type'      => 'select',
    'choices'   => $choices
);

//add the control
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'ounox-fonts-display-control', $args));

多谢了,男人很有魅力对不起,我真的是个后台高手。我真的很感激男人@马克·安东尼:)乐意帮忙:)