【问题标题】:Select Custom Post Types Per Category?选择每个类别的自定义帖子类型?
【发布时间】:2018-01-06 18:52:52
【问题描述】:

尝试按所选类别选择自定义帖子

 $args = array('post_type' => 'speakers', 'posts_per_page' => 10,

                            //'category_name' => 'saturday',// <- solution A
//                            'tax_query' => array(
//                                array(
//                                    'taxonomy' => 'category',
//                                    'field'    => 'tag_ID',
//                                    'terms'    => array(2),
//                                ),
//                            ),//<- solution B
//                            'cat' => 2,//<- solution C

                            'tag__in' => array( 2 ) //<- solution D
//                            'tax_query' => array(
//                                array(
//                                    'taxonomy' => 'category',
//                                    'field' => 'tag_ID', //can be set to ID
//                                    'terms' => '2' //if field is ID you can reference by cat/term number
//                                )
//                            )//<- solution E
                        );

我只是为帖子类型添加了 2 个类别,但所有自定义帖子都被选中,不仅是类别 id 为 2 的帖子

所有 5 个解决方案都不起作用,我还通过保存和重新保存来刷新永久链接

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    试试这个,

    <?php 
           $args = array('post_type' => 'speakers', 'posts_per_page' => 10, 
                    'tax_query' => array(
                      array(
                        'taxonomy' => 'custom_taxonomy',
                        'field'    => 'slug',
                        'terms'    => 'saturday',
                      ),
                    ),
                  ); ?>
    

    尝试更改帖子类型“演讲者”的自定义分类,因为“类别”是 WP 帖子的 slug 名称。

    【讨论】:

      猜你喜欢
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2013-05-12
      • 2015-10-11
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多