在定制器下拉列表中添加帖子类型标题

时间:2015-12-02 作者:Tom

我想将可用联系人表单列表(在联系人表单7中创建)添加到自定义程序的下拉列表中。

以下是我使用的代码:

function contact_form_list() {
    $args = array(\'post_type\' => \'wpcf7_contact_form\');
    $cf7forms = get_posts( $args );
    foreach($cf7forms as $cf7form) {
            return $cf7form->post_title;
    }
}

// Add setting and control for Contact section

$wp_customize->add_setting( \'contact_list\', array(
    \'transport\' => \'postMessage\'
));
$wp_customize->add_control( \'contact_list\', array(
    \'label\'    => esc_html__( \'Contact form\', \'mytheme\' ),
    \'type\'     => \'select\',
    \'section\'  => \'contact_section\',
    \'priority\' => 4,
    \'choices\'  => contact_form_list()
));
它显示下拉列表,但没有选择。它是空的。

我做错了什么?还是这样做是错误的?

谢谢你的帮助!

1 个回复
SO网友:Tom

好的,我找到了,下面是下一个寻找同样东西的人的答案:

$cf7forms_list = array();
$args = array(\'post_type\' => \'wpcf7_contact_form\');
$cf7forms = get_posts( $args ); 
foreach($cf7forms as $cf7form) {
    $cf7forms_list[$cf7form->post_title] = $cf7form->post_title;
}

$wp_customize->add_setting( \'contact_list_section\', array(
    \'transport\' => \'postMessage\'
));
$wp_customize->add_control( \'contact_list_section\', array(
    \'label\'    => esc_html__( \'Contact form\', \'mytheme\' ),
    \'type\'     => \'select\',
    \'section\'  => \'contact_section\',
    \'priority\' => 4,
    \'choices\'  => $cf7forms_list,
));