2017-02-23 12 views
0

このトピックに関する他の記事を見てきましたが、私のコードがそのように動作している理由が見つかりません。カテゴリリストをループして、各カテゴリから最新の1つの投稿を選択しています。私が持っている問題は、たとえ私が 'posts_per_page' => 1を使っていても、そのコードがそのカテゴリのすべての投稿を返すことです。Wordpress WP_Query posts_per_page正しい投稿数が返されない

<?php $categories = get_categories(array('exclude'=>array(1, 8))); 
foreach ($categories as $category) { 
    $args = array(
     'cat' => $category->term_id, 
     'posts_per_page' => 1, 
     'orderby' => 'date' 
    ); 
    $query = new WP_Query($args); 
    while($query->have_posts()):$query->the_post(); ?> 
     <li> 
      <a href="<?php the_permalink(); ?>"> 
       <div class="post-cat"><span>Latest</span> <?php echo $cat>name; ?></div> 
       <div class="post-image"><img src="<?php the_post_thumbnail_url('large'); ?>" alt="<?php the_title(); ?>" /></div> 
       <div class="post-title"><?php the_title(); ?></div> 
       <?php the_excerpt();?> 
       <div class="post-date"><?php the_time('F jS, Y'); ?></div> 
      </a> 
     </li> 
    <?php endwhile; ?> 
<?php } ?> 
<?php wp_reset_postdata(); ?> 

例:ここで私は以下の使用していたコードがある

私はどのようにするというカテゴリがあると私は、そのカテゴリの最新記事を返すことになっていますが、上記のコードは2が表示されていますそのカテゴリがチェックされている投稿。

EDIT:この質問を見ているあなたのそれらのため

Array ([0] => WP_Term Object ([term_id] => 3 [name] => Events [slug] => events [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 3 [category_count] => 1 [category_description] => [cat_name] => Events [category_nicename] => events [category_parent] => 0) [1] => WP_Term Object ([term_id] => 10 [name] => How To [slug] => how-to [term_group] => 0 [term_taxonomy_id] => 10 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw [cat_ID] => 10 [category_count] => 2 [category_description] => [cat_name] => How To [category_nicename] => how-to [category_parent] => 0) [2] => WP_Term Object ([term_id] => 4 [name] => Interviews [slug] => interviews [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw [cat_ID] => 4 [category_count] => 2 [category_description] => [cat_name] => Interviews [category_nicename] => interviews [category_parent] => 0) [4] => WP_Term Object ([term_id] => 2 [name] => People [slug] => people [term_group] => 0 [term_taxonomy_id] => 2 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 2 [category_count] => 1 [category_description] => [cat_name] => People [category_nicename] => people [category_parent] => 0) [6] => WP_Term Object ([term_id] => 5 [name] => Results [slug] => results [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 5 [category_count] => 1 [category_description] => [cat_name] => Results [category_nicename] => results [category_parent] => 0)) 
+0

args配列に 'ignore_sticky_posts '=> 1'を追加してみてください。 –

+0

最後にwp_reset_postdata()を配置しますか? ? –

+0

粘着性のあるポストを無視しようとしましたが、同じ結果が得られました。 Mohammadあなたはwp_resetがforeachループの中に入る必要があると思いますか? –

答えて

0

:ここ は$カテゴリ要求の結果です。私は以下のコードを使用して、最新のを示すために、それを強制する必要がありました:

<?php $categories = get_categories(array('exclude'=>array(1, 8))); 
    foreach ($categories as $category) { 
     $args = array(
      'category' => $category->term_id, 
      'numberposts' => 1, 
      'orderby'=>'date' 
     ); 
     $query = get_posts($args); 
     $i = 0; 
     foreach($query as $post):setup_postdata($post); ?> 
      <?php if($i == 0) { ?> 
       <li> 
        <a href="<?php the_permalink(); ?>"> 
         <div class="post-cat"><span>Latest</span> <?php echo $category->name; ?></div> 
         <div class="post-image"><img src="<?php the_post_thumbnail_url('large'); ?>" alt="<?php the_title(); ?>" /></div> 
         <div class="post-title"><?php the_title(); ?></div> 
         <?php the_excerpt();?> 
         <div class="post-date"><?php the_time('F jS, Y'); ?></div> 
        </a> 
       </li> 
      <?php } $i++; ?> 
     <?php endforeach; ?> 
    <?php } ?> 
    <?php wp_reset_postdata(); ?> 

は、私は少し違っそれを設定し、私はそれが唯一の各ループの最初の発生を使用チェック$を追加しました。これはおそらくこれを行う最も効率的な方法ではないでしょう。もし誰かがコードをより効率的にする提案があれば、私はそれを好きになるでしょう。

関連する問題