2016-09-30 83 views
0

我有一些任務試圖解決可能不是最聰明的方式,但到目前爲止,我只有這個想法。我有5 自定義帖子類型。在每個這個 - 一個職位。想要這些帖子循環出現在我的index.php。這些帖子有the_field()功能從ACF插件。 結果不是我所期望的。這是的index.php如何循環槽自定義帖子類型並獲得他們的內容在一個頁面

<?php 

     $posttypes = array('plays','watch','news','shop','contact'); 
     $args = array(
      'post_type' => $posttypes 
     , 'posts_per_page' => 5); 
     $the_query = new WP_Query($args); 
     ?> 
     <?php if ($the_query->have_posts()) : ?> 

     <div id="owl-demo" class="owl-carousel owl-theme"> 
     <?php foreach($posttypes as $posttype){?> 
      <?php $the_query->the_post(); ?> 
     <div class="item"> 
      <?php get_template_part('single', $posttype); ?> 
     </div> 
      <?php };?> 

      <?php wp_reset_postdata(); ?> 

     <?php else: ?> 
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
      <?php endif; ?> 
      </div> 

另外的代碼,我將發佈後的代碼中的一個,像single_news.php

<?php get_header(); 

而(have_posts()):the_post() ;?>

<div class="container-fluid "> <!-- play section --> 

        <div class="row"> 
         <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2"> 
          <h2 class="" style="text-transform:uppercase"><?php the_field('name');?></h2> 
         </div> 
        </div> 
     <div class="row"> 
      <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2"> 
       <h2 class="" style="text-transform:uppercase"><?php the_field('news_name');?></h2> 
      </div> 
     </div> 
</div> <!-- row --> 
     </div> 

其實這個文件的最後,我想不出後但它本身正確結束潛水並結束。

回答

0
In args you have post_per_page set to 5 – this means it will only return the 5 latest posts, regardless of Custom Post Type (CPT). 
Try '-1' (return everything) for debugging. 

`$args = array(
    'post_type' => $posttypes, 
    'posts_per_page' => -1 
);` 

Do you want it to return 1 latest post from each CPT? 

Maybe try this... 

<?php while($the_query->have_posts()) : ?> 
    <?php $the_query->the_post(); ?> 
    <div class="item"> 
     <?php get_template_part('single', get_post_type()); ?> 
    </div> 
<?php endwhile; ?> 


Or run query for each posttype... 

<?php foreach($posttypes as $posttype) : ?> 
    <?php 
    $args = array(
     'post_type' => $posttype, 
     'posts_per_page' => 5); 
    $the_query = new WP_Query($args); 
    ?> 
    <?php while($the_query->have_posts()) : ?> 
     <?php $the_query->the_post(); ?> 
     <div class="item"> 
      <?php get_template_part('single', $posttype); ?> 
     </div> 
    <?php endwhile; ?> 
<?php endforeach; ?> 
//I think endforeach exists, otherwise use curly braces 

Not sure exactly what you want without seeing it visually. 
+0

嗨傑拉德。感謝您的迴應。你的例子帶來了相同的。其實我想讓所有的帖子都顯示/帶來。事情是,我希望他們在旋轉木馬,這個類=「貓頭鷹旋轉木馬做它,其中5個是在一個頁面中,所以我可以旋轉木馬通過他們。我認爲我做了錯誤的foreach –

+0

我已經更新了我的答案 - 格式化的原型;我是新發布的StackOverflow –

相關問題