2016-09-30 5 views
0

私は解決しようとするいくつかのタスクが賢い方法ではない可能性がありますが、これまで私はこのアイデアを得た。私は5 カスタムポストタイプを持っています。これのそれぞれで1つの投稿。これらの投稿をループし、私のindex.phpに表示したい。これらの投稿はthe_field()機能からACFプラグインです。 結果は私が期待したものではありません。これは、index.phpをどのようにループトラフのカスタム投稿の種類と1つのページでその内容を取得

<?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> 

のコードであるような

<?php get_header(); 

single_news.phpしばらく(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> 

は実際にこのファイルの最後には、私はポストをcouldntはそれ自体、正しいです終了ダイブと終わり。

答えて

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

私の答えを更新しました - フォーマットのためのapoogies;私はStackOverflowを投稿するのは初めてです –

関連する問題