2017-02-19 71 views
0

我在展示何時聲明'posts_per_page'時顯示多個帖子時遇到問題。自定義WP_Query'posts_per_page'不起作用

在我的研究中,我的代碼已經開發出來,但仍然沒有結果。我試過

'ignore_sticky_posts' => 1'nopaging' => true(分頁)

切換主題和停用插件。我似乎無法找到問題。任何幫助將非常感激。這裏是當前的結果 - alderneyfootball.co.uk

我在頁面上有兩個循環,它是一個自定義頁面模板。我正在使用WP-Club-Manager Plugin

<?php 
    // the query 
    wp_reset_query(); 

    $wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1); 

    $wpb_next_game = new WP_Query($wpb_next_game); 
     if (have_posts()) : while ($wpb_next_game->have_posts()) : $wpb_next_game->the_post(); 

      $date = date_i18n(get_option('date_format'), strtotime($post->post_date)); 
      $time = date_i18n(get_option('time_format'), strtotime($post->post_date)); 
      $side = wpcm_get_match_clubs($post->ID); 

      // badge 
      $badges = wpcm_get_match_badges($post->ID, 'crest-medium', array('class' => 'home-logo')); 
      $badges = wpcm_get_match_badges($post->ID, 'crest-medium', array('class' => 'away-logo')); 
      $format = get_match_title_format(); 
      if($format == '%home% vs %away%') { 
       $badge = $badges[0]; 
      } else { 
       $badge = $badges[1]; 
      } 
      ?> 

     <div id="next-match" class="row"> 
     <div class="span_6 col"> 
      <h3 class="next-match-home"><?php echo $side[0]; ?></h3> 
     </div> 
     <div class="span_6 col"> 
      <h3 class="next-match-away"><?php echo $side[1]; ?></h3> 
     </div> 
     </div> 
     <div class="row"> 
      <h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4> 
     <?php endwhile; ?> 
     <!-- end of the loop --> 
    <?php else : ?> 
     <h4 id="next-match-time"><?php _e('Sorry, scheduled matches'); ?></h4> 
    <?php endif; ?> 

回答

1

如果兩個循環的數據被超載,那麼我的第一個代碼wp_reset_query()是不正確的。如果你正在使用WP_Query然後

wp_reset_postdata() //remove wp_reset_query() which is used for wp_query() 

應該while循環的結束,這意味着在你的兩個循環,你必須有

wp_reset_postdata() // use this at both loops 

在兩個循環中,而結束後使用循環。

現在,你的代碼看起來是這樣的:

<?php 

$wpb_next_game = array('post_type'=>'wpcm_match', 'post_status' => 'future', 's' => 'Alderney', 'posts_per_page' => 1); 

$wpb_next_game = new WP_Query($wpb_next_game); 
    if (have_posts()) : while ($wpb_next_game->have_posts()) : $wpb_next_game->the_post(); 

     $date = date_i18n(get_option('date_format'), strtotime($post->post_date)); 
     $time = date_i18n(get_option('time_format'), strtotime($post->post_date)); 
     $side = wpcm_get_match_clubs($post->ID); 

     // badge 
     $badges = wpcm_get_match_badges($post->ID, 'crest-medium', array('class' => 'home-logo')); 
     $badges = wpcm_get_match_badges($post->ID, 'crest-medium', array('class' => 'away-logo')); 
     $format = get_match_title_format(); 
     if($format == '%home% vs %away%') { 
      $badge = $badges[0]; 
     } else { 
      $badge = $badges[1]; 
     } 
     ?> 

    <div id="next-match" class="row"> 
    <div class="span_6 col"> 
     <h3 class="next-match-home"><?php echo $side[0]; ?></h3> 
    </div> 
    <div class="span_6 col"> 
     <h3 class="next-match-away"><?php echo $side[1]; ?></h3> 
    </div> 
    </div> 
    <div class="row"> 
     <h4 id="next-match-time">Next Match at <?php echo $time; ?> on <?php echo $date; ?></h4> 
    <?php endwhile; 
    wp_reset_postdata(); 
     ?> 
    <!-- end of the loop --> 
<?php else : ?> 
    <h4 id="next-match-time"><?php _e('Sorry, scheduled matches'); ?></h4> 
<?php endif; ?> 

希望這對你的作品

謝謝

+0

謝謝您的回答。不幸的是它沒有解決這個問題。但是,由於您的代碼是正確的,我認爲這是一個主題問題。我的追捕仍在繼續,但我向前邁了一步 – Wiseguy

+0

找到了答案。我使用的主題叫做'Sailent',他們仍然使用分頁參數'showposts',它自wordpress 2.1版本以來由'posts_per_page''取代 – Wiseguy