最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Post Pagination Showing Same Posts Every Page at wordpress blog

programmeradmin4浏览0评论

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">&larr;</span> Older posts', 'BTC' ) ); ?></div>
                    <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</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">&larr;</span> Older posts', 'BTC' ) ); ?></div>
                    <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</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 Is there a reason you're calling get_posts() and not using the main query? – WebElaine Commented Apr 20, 2020 at 21:48
  • I don't know sir @WebElaine it's not me did this design – Ahmed Tawfek Commented Apr 20, 2020 at 21:51
Add a comment  | 

1 Answer 1

Reset to default 2

Whenever 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">&larr;</span> Older posts', 'BTC' ) ); ?></div>
        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'BTC' ) ); ?></div>
    </div><!-- #nav-below -->
<?php endif; ?>
发布评论

评论列表(0)

  1. 暂无评论