列出 wordpress 主题定制器中的帖子

List posts in the wordpress theme customiser

你好。 我正在创建一个 Wordpress 主题,想知道是否有人知道在主题定制器中列出我的自定义 post 类型之一的方法。我有一个名为 "slideshow" 的自定义 post 类型,它具有自定义元框等,专为幻灯片放映而设计。我希望能够在 customiser 的下拉列表中列出这些 post。理想情况下以这样的数组结束它们...... 'the_id' => 'Slideshow post title',

            $wp_customize->add_setting(
              'slideshow-homepage',
              array(
                'default' => 'none',
               )
              );

            $wp_customize->add_control(
              'slideshow-homepage',
              array(
                'type' => 'select',
                'priority' => 3,
                'label' => 'Slideshow',
                'description' => '',
                'section' => 'homepage',
                'choices' => array(

                'somehow' => 'somehow',
                'list' => 'list',
                'all' => 'all',
                'custom' => 'custom',
                'post' => 'post',
                'types' => 'types',
                'of' => 'of',
                'type' => 'type',
                'slideshow' => 'slideshow'

                ),
              )
            );

非常感谢大家。刘易斯

使用array_reduce

$wp_customize->add_control(
              'slideshow-homepage',
              array(
                'type' => 'select',
                'priority' => 3,
                'label' => 'Slideshow',
                'description' => '',
                'section' => 'homepage',
                'choices' => array_reduce( 
                    get_posts( 'post_type=slideshow&posts_per_page=-1' ), 
                    function( $result, $item ) { 
                        $result[$item->ID] = $item->post_title;
                        return $result;
                    }
                ),
              )
            );

还要添加一个空字段:

$posts = array_reduce( 
        get_posts( 'post_type=slideshow&posts_per_page=-1' ), 
        function( $result, $item ) { 
            $result[$item->ID] = $item->post_title;
            return $result;
        }
    );
    $none = array('' => 'None');
    $choices = $none + $posts;

    $wp_customize->add_control('slideshow_control', array(
        'label'      => __('Choose Slideshow', 'themename'),
        'section'    => 'slideshow_option',
        'settings'   => 'slideshow_settings',
        'type' => 'select',
        'choices' => $choices
    ));