2015-08-25 184 views
4

我正在尝试在网站上制作“一个月的餐”部分。该菜单被分成自定义帖子类型,所以我需要能够循环多个帖子类型的类别。从多个自定义帖子类型中显示某个类别的帖子

这是代码我迄今这也绝对没有什么:

<div class="maaltijd-vdm col-1-1"> 
       <?php $mvdm = new WP_Query(array('category_name' => 'mvdm', 'posts_per_page' => 1)); ?> 
       <?php while ($mvdm->have_posts()) : $mvdm->the_post(); ?> 

        <div class="mvdm-thumb"> 
         <?php the_thumbnail(); ?> 
        </div> 

        <div class="description"> 
         <h3><?php the_title(); ?></h3> 
         <p><?php get_the_mvdm(); ?></p> 
        </div> 

       <?php endwhile; wp_reset_postdata(); ?> 

      </div> 

我将非常感谢您的帮助!

* get_the_mvdm是一个自定义功能

*我已经在使用相同的代码相同的页面的新闻循环(除了变量名)

回答

3

要查询多个posttypes,你可以传递数组查询后的类型slu gs。

$args = array(
    'post_type'   => array('cpt1', 'cpt2'), /* the names of you custom post types */ 
    'category_name'  => 'mvdm', 
    'posts_per_page' => -1      /* get all posts */ 
) 

$mvdm = new WP_Query($args); 
+0

非常感谢您!它的工作原理:) –

+0

如何在这个循环中显示与每个帖子相关的分类 –

+0

请打开一个关于此主题的新问题。 – xphan

1

您必须使用tax_query按类别获取帖子。

试试这个代码:

$tags_args = array(
       'post_type' => array(cpt1, cpt2, cpt3 ....), 
       'posts_per_page' => 999, 
       'order' => 'DESC', 
       'tax_query' => array(
            array(
             'taxonomy' => 'Your Taxonomy', 
             'field' => 'slug', 
             'terms' => 'Your term slug' 
            ) 
           ) 
          ); 
      $tags_qry = new WP_Query($tags_args); 

      while($tags_qry->have_posts()) : 
       $tags_qry->the_post(); 

       // Your Code 
      endwhile 

希望你找到你的解决方案。

+0

感谢您的回答! –

0

这为我工作:

$args = array(
    'post_type' => array(
     'news', 
     'agenda' 
    ), 
    'posts_per_page' => '8', 
    'tax_query' => array(
     'relation' => 'OR', 
     array(
      'taxonomy' => 'news_cat', 
      'field' => 'slug', 
      'terms' => 'bedrijven' 
     ), 
     array(
      'taxonomy' => 'agenda_cat', 
      'field' => 'slug', 
      'terms' => 'bedrijven' 
     ) 
    ) 
); 
相关问题