自定义WP_Query' posts_per_page'不管用

时间:2017-02-19 15:51:26

标签: php wordpress

我在发布多个帖子时遇到问题,这些帖子显示在我发布的帖子_per_page'之后。

在我研究期间,我的代码已经开发但仍然没有结果。我试过了

'nopaging' => true<?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; ?> (分页)

切换主题和停用插件。我似乎无法找到问题。任何帮助将非常感激。以下是当前结果 - this

我在页面上有两个循环,它是一个自定义页面模板。我正在使用alderneyfootball.co.uk

mergeSort([4, 3, 2, 1])

2 个答案:

答案 0 :(得分: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

在while循环结束时的两个循环中。

现在您的代码如下所示:

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

希望这适合你

谢谢

答案 1 :(得分:0)

也许你的下一个循环需要从之前的查询。 确保您将wp_reset_query()用于query_posts和/或wp_reset_postdata()用于wp_query

每次循环后。