簡體   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