【问题标题】:List posts in the wordpress theme customiser在 wordpress 主题定制器中列出帖子
【发布时间】:2016-02-14 20:16:23
【问题描述】:

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

            $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'

                ),
              )
            );

非常感谢男孩和女孩。刘易斯

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    使用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
        )); 
    

    【讨论】:

    • 不确定您的帖子类型叫什么。很高兴它成功了。
    • 工作得很好,谢谢你,你会知道如何在缩减数组中包含默认值吗?即“无”=>“无”......再次感谢
    • 哦,你绝对的明星!我只是在查看 array_merge 的 php 手册中,但你让我明白了!我非常感谢像你这样知道你在说什么的人!
    • 很抱歉一直打扰您,我真的很抱歉,非常感谢您的帮助。您是否可能知道一种获取帖子 ID 的方法,而不是让数组以递增方式列出数字作为 ID。目前,我可以从下拉列表中选择并呈现一个 ID,但是当我添加“幻灯片”类型的新帖子时,所选 ID 会发生变化。这有意义吗?
    • 哦,我明白了。我更新了我的答案。而不是array_merge 使用+ 运算符
    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多