Pagination for blog posts reloading same page

Symptoms

Visiting /blog/ loads the first 10 posts, as expected.

Read More

Visiting /blog/page/2/ (and onward) loads the new URL but with the same 10 posts. Nothing changes but the URL.

Setup Details

Custom Permalink Structure: /blog/%year%/%monthnum%/%day%/%postname%/

Static Posts Page: “Blog” (slug of ‘blog’)

I’m using home.php to displays posts with the following markup:

<?php
/**
 * The archive for blog posts 
*/

get_header(); ?>

    <div id="main-content">
        <div class="container">

            <section id="content" class="blog"> 

                <?php while ( have_posts() ) : the_post(); ?>

                    <?php get_template_part( 'content', 'blog-index' ); ?>

                <?php endwhile; ?>

            </section>

            <?php get_sidebar( 'blog' ); ?>

            <div class="pagination">
                <?php wp_pagenavi(); ?>
            </div>

        </div>
    </div>

<?php get_footer(); ?>

I’ve checked for plugin conflicts and I am running a relatively minimalistic setup. I’m using WP-PageNavi to generate the pagination.

I’ve also tried renaming the /blog/ page to ‘blog2’ with the thought that there might be a permalink conflict – same result. /blog2/page/2/ loads the same first 10 posts.

Any ideas what might be the cause here?

Related posts

5 comments

  1. $my_query = new WP_Query( array( 'posts_per_page' => 3, 'paged' => get_query_var('paged') ) );
    
    while ( $my_query->have_posts() ) : $my_query->the_post();
        the_title();
        // more stuff here
    endwhile;
    
    wp_pagenavi( array( 'query' => $my_query ) );
    
    wp_reset_postdata();
    

    try this code and also increase the size of show posts in SETTINGS->Reading->Blog pages show at most 10 to 50 or 100, hope so it will help you.

  2. Why are you using WP-PageNavi for number based pagination. WordPress offer a way to accomplish that. BTW here is the full query for you to use in home.php

    <div id="main-content">
        <div class="container">
    
            <section id="content" class="blog"> 
    
                <?php
    
                  if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; }
    
                  $args = array(
                      'post_type' => 'post',
                      'paged' => $paged
                  );
    
                  $my_query = new WP_Query( $args );
    
                  if ( $my_query->have_posts() ) :
    
                      while ( $my_query->have_posts() ) : $my_query->the_post();
    
                          get_template_part( 'content', 'blog-index' );
    
                      endwhile;
    
                      if ( $my_query->max_num_pages > 1 ) :
                          $big = 999999999;
                          echo '<div class="pagination">';
                          echo paginate_links( array(
                              'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                              'format' => '?paged=%#%',
                              'current' => max( 1, get_query_var('paged') ),
                              'total' => $my_query->max_num_pages
                          ) );
                          echo '</div>';
                      endif;
    
                  else :
    
                        get_template_part( 'content', 'none' );
    
                  endif;
    
                  wp_reset_postdata();
    
                ?>
    
            </section>
    
            <?php get_sidebar( 'blog' ); ?>
    
        </div>
    </div>
    
  3. Please use this code I faced same problem then i implemented this

    <?php if ( $wp_query->max_num_pages > 1 ) :?>
        <div class="pagination">
            <?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) { 
                $link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
                echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
            } ?>
        </div>
    <?php endif ?>
    

    Hope it helps you too.

  4. Using home.php doesn’t sound like a good idea, but if it is the only query in the page, you might have to add a couple of things. The following usually works for paginating a static page:

    Add:

    global $paged, $wp_query; 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    

    But you also have to add one more argument to your query arguments in your template part to enable pagination:

    'paged'     => $paged
    

    Your final code on the template would be:

    get_header(); ?>
    
    global $paged, $wp_query; 
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
        <div id="main-content">
            <div class="container">
    
            <section id="content" class="blog"> 
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php get_template_part( 'content', 'blog-index' ); ?>
    
                <?php endwhile; ?>
    
            </section>
    
            <?php get_sidebar( 'blog' ); ?>
    
            <div class="pagination">
                <?php wp_pagenavi(); ?>
            </div>
    
        </div>
    </div>
    
  5. Why don’t you force the pagination by adding the following code like so:

    <div id="main-content">
        <div class="container">
    
            <section id="content" class="blog"> 
    
                <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts('posts_per_page=3&paged=' . $paged);
                ?>
    
                <?php while ( have_posts() ) : the_post(); ?>
    
                    <?php get_template_part( 'content', 'blog-index' ); ?>
    
                <?php endwhile; ?>
    
            </section>
    
            <?php get_sidebar( 'blog' ); ?>
    
            <div class="pagination">
                <?php wp_pagenavi(); ?>
            </div>
    
        </div>
    </div>
    

    Check this link: http://codex.wordpress.org/Function_Reference/query_posts#Pagination

Comments are closed.