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; ?> ``
  • 1
    Is there a reason you're calling get_posts() and not using the main query? – WebElaine Apr 20 at 21:48
  • I don't know sir @WebElaine it's not me did this design – Ahmed Tawfek Apr 20 at 21:51
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.com/blog WordPress will query X number (defined in Settings > Reading) of your latest posts. Then if you visit example.com/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; ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.