简体   繁体   English

自定义 WP_Query 'posts_per_page' 不工作

[英]Custom WP_Query 'posts_per_page' is not working

I'm having trouble with multiple posts showing when I've declared 'posts_per_page'.当我声明“posts_per_page”时,我遇到了显示多个帖子的问题。

During my research my code has developed but with still no results.在我的研究过程中,我的代码已经开发,但仍然没有结果。 I've tried我试过了

'ignore_sticky_posts' => 1 , 'nopaging' => true (pagination) 'ignore_sticky_posts' => 1 , 'nopaging' => true (分页)

switching theme and deactivating plugins.切换主题和停用插件。 I can't seem to find the issue.我似乎找不到问题所在。 Any help would be much appreciated.任何帮助将非常感激。 Here is the current result - alderneyfootball.co.uk这是当前结果 - alderneyfootball.co.uk

I have two loops on the page and it's a custom page template.我在页面上有两个循环,它是一个自定义页面模板。 I'm using the WP-Club-Manager Plugin我正在使用WP-Club-Manager 插件

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

If the two loops data is overwridden then, i your first code wp_reset_query() is incorrect.如果两个循环数据被覆盖,那么我的第一个代码 wp_reset_query() 是不正确的。 If you are using WP_Query then如果您使用的是 WP_Query 那么

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

should be used after the end of the WHILE loop which means that in your two loops you have to have应该在 WHILE 循环结束后使用,这意味着在你的两个循环中你必须有

wp_reset_postdata()  // use this at both loops

at both loops in the end of the while loop.在 while 循环末尾的两个循环中。

Now you codes looks like this:现在你的代码看起来像这样:

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

Hope this works for you希望这对你有用

Thank You谢谢你

Perhaps your next loop takes query from previous.也许您的下一个循环需要来自上一个的查询。 Make sure you're using wp_reset_query() for query_posts and/or wp_reset_postdata() for wp_query确保您对 query_posts 使用wp_reset_query()和/或对 wp_query 使用 wp_reset_postdata( )

After each loop.在每个循环之后。

I was facing same issue.我面临着同样的问题。 If you have added below in code then remove it and it will work for you.如果您在代码中添加了下面的代码,请将其删除,它将为您工作。

wp_reset_postdata() 

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM