2015-08-25 180 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' 
     ) 
    ) 
); 
相關問題