2017-02-23 96 views
0

我已經瀏覽了關於這個主題的其他文章,找不到任何理由爲什麼我的代碼的行爲是這樣的。我正在循環瀏覽我的類別列表,並從每個類別中選擇單個最新帖子。我遇到的問題是即使我使用'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(); ?> 

例子:

我有一個叫我如何和應該是返回從該類別中的最新職位,但上面的代碼顯示的是兩個類別檢查了該類別的帖子。

編輯: 這裏是$類別的結果要求:

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

嘗試添加''ignore_sticky_posts'=> 1'到你的args數組。 –

+0

你會在結束後放置wp_reset_postdata()嗎? ? –

+0

嘗試忽略粘性帖子,但得到相同的結果。穆罕默德你認爲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(); ?> 

我把它有點不同,並且增加了$我檢查,因此只能用於每個循環中的第一次出現。這可能不是最有效的方法來做到這一點,如果任何人有建議,使代碼更有效我會喜歡。