使用2个循环显示内容后,分页中断

时间:2019-06-10 作者:R Reveley

我想在博客的第一篇文章中使用不同的HTML。

我做了两个循环。不幸的是,这已停止分页。我想这是因为我正在创建一个新的查询来解决这个问题。

如何在仍有工作分页的情况下解决问题?

原始单循环代码

    <?php # The Loop    
    if ( have_posts()): ?>    
        <div class="row">    
            <?php # Loop through    
                while ( have_posts() ) : the_post();    
                    include( \'includes/snippets/\'.$post->post_type.\'-card.php\');    
            endwhile; ?>    
        </div>    
    <?php else:     
        echo \'<div class="alert alert-warning">No results found.</div>\';    
    endif; ?>  
我的双环尝试

 <?php
 $args = array(
    \'posts_per_page\' => \'1\',
 );
 $query = new WP_query ( $args );
 if ( $query->have_posts() ) { ?>
 <?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>
 <?php include( \'includes/snippets/\'.$post->post_type.\'-card-large.php\'); ?>
 <?php // End the loop.
 endwhile;
 } 

 $args = array(
    \'posts_per_page\' => \'11\',
    \'offset\' => \'1\',
 );
 $query = new WP_query ( $args );
 if ( $query->have_posts() ) { ?>
 <?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>
 <?php include( \'includes/snippets/\'.$post->post_type.\'-card.php\'); ?>
 <?php // End the loop.
 endwhile;
 } ?>

 <div class="nav-previous"><?php next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\', \'twentyten\' ) ); ?></div>
 <div class="nav-next"><?php previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\', \'twentyten\' ) ); ?></div>

1 个回复
最合适的回答,由SO网友:ChristopherJones 整理而成

这是我的尝试。。。虽然我真的没有一个好的地方来测试它,但我认为它应该起作用。(祈祷好运)

<?php
  // First Page of Pagination - https://codex.wordpress.org/Function_Reference/is_paged
  if(!is_paged()){
    $first_page = true;
    $query_args = array(
       \'posts_per_page\' => \'11\',
    );
  } else {
    $query_args = array(
       \'posts_per_page\' => \'10\',
       \'offset\' => \'1\',
    );
  }

  $query = new WP_query ( $query_args );
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
      // Let\'s deal with the first post on the first page
      if($first_page===true && $wp_query->current_post===0){
        include( \'includes/snippets/\'.$post->post_type.\'-card-large.php\');
      } else {
        include( \'includes/snippets/\'.$post->post_type.\'-card.php\');
      }
    endwhile;
  }

?>

<div class="nav-previous"><?php next_posts_link( __( \'<span class="meta-nav">&larr;</span> Older posts\', \'twentyten\' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( \'Newer posts <span class="meta-nav">&rarr;</span>\', \'twentyten\' ) ); ?></div>
试过之后让我知道你那边的情况,看看我们是否能解决!

相关推荐

Last post in loop when even

我使用这段代码尝试检查每个页面上的最后一篇文章循环是否均匀,以便添加不同的类。这可以检查它是否是最后一篇文章:( ( 1 == $wp_query->current_post + 1 ) == $wp_query->post_count ) 这可以检查它是否是一个均匀的帖子( $wp_query->current_post % 2 == 0 ) 但这并没有检查这是否是最后一篇文章,甚至。 ( ( 1 == $wp_query->current_post + 1