繁体   English   中英

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

[英]List posts in the wordpress theme customiser

嘿。 我正在创建一个Wordpress主题,想知道是否有人知道一种在主题定制器中列出我的自定义帖子类型的方法。 我有一个名为“ slideshow”的自定义帖子类型,该类型具有自定义的meta框等,并且仅用于幻灯片演示。 我希望能够在定制程序的下拉列表中列出这些帖子。 理想的情况是像这样在数组中以它们结尾...'the_id'=>'Slideshow帖子标题',

            $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
    )); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM