admin管理员组文章数量:1387428
at WordPress blog Post Pagination Showing Same Posts Every Page
My loop
<?php
$args = array( 'numberposts' => 12, 'order'=> 'DESC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="blogrollentry">
<p class="blogrolltitle"><?php the_title(); ?></p>
<div class="container-fluid no-padding">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('post-thumbnail', array('class' => 'blogpostimage1')); } else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/temp-h.jpg" class="blogpostimage1" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<p><?php echo intro_textblog(350); ?></p>
<a class="btn btn-block btn-outline-dark my-2 my-sm-0 biggertext" href="<?php echo get_permalink(); ?>">Read More</a>
</div>
<?php endforeach; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'BTC' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'BTC' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?> ``
at WordPress blog Post Pagination Showing Same Posts Every Page
My loop
<?php
$args = array( 'numberposts' => 12, 'order'=> 'DESC', 'orderby' => 'date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="blogrollentry">
<p class="blogrolltitle"><?php the_title(); ?></p>
<div class="container-fluid no-padding">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('post-thumbnail', array('class' => 'blogpostimage1')); } else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/temp-h.jpg" class="blogpostimage1" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<p><?php echo intro_textblog(350); ?></p>
<a class="btn btn-block btn-outline-dark my-2 my-sm-0 biggertext" href="<?php echo get_permalink(); ?>">Read More</a>
</div>
<?php endforeach; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'BTC' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'BTC' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?> ``
Share
Improve this question
edited Apr 23, 2020 at 6:27
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Apr 20, 2020 at 20:41
Ahmed TawfekAhmed Tawfek
131 bronze badge
2
|
1 Answer
Reset to default 2Whenever you visit a URL in WordPress, WordPress automatically queries the correct posts for you based on that URL and your settings. For example if you visit example/blog
WordPress will query X number (defined in Settings > Reading) of your latest posts. Then if you visit example/blog/page/2
WordPress will query your latest posts offset by X, to get the second page. This is generally referred to as the "main" query.
To loop through posts in the main query, you use The Loop:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
Notice how there's no get_posts()
or WP_Query
. WordPress has already queried the correct post/s for that URL, so you just need to use the standard loop.
The issue with your template is that your pagination links are based on the main query, but the template is otherwise only displaying the results of get_posts()
. So if you go to /page/2
, WordPress will query the correct posts for page 2, but instead of displaying them your template is displaying the results of a completely separate query performed with get_posts()
, which is not changing its arguments based on the current page, so they'll always be the same posts.
The solution is to remove any reference to the custom query and use the main loop:
<?php while ( have_posts() ) : the_post() ; ?>
<div class="blogrollentry">
<p class="blogrolltitle"><?php the_title(); ?></p>
<div class="container-fluid no-padding">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('post-thumbnail', array('class' => 'blogpostimage1')); } else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/temp-h.jpg" class="blogpostimage1" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<p><?php echo intro_textblog(350); ?></p>
<a class="btn btn-block btn-outline-dark my-2 my-sm-0 biggertext" href="<?php echo get_permalink(); ?>">Read More</a>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'BTC' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'BTC' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>
本文标签: Post Pagination Showing Same Posts Every Page at wordpress blog
版权声明:本文标题:Post Pagination Showing Same Posts Every Page at wordpress blog 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744540995a2611611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_posts()
and not using the main query? – WebElaine Commented Apr 20, 2020 at 21:48