2

So I've been looking into why the pagination isn't working, but I just can't find anything. When it goes to page 2, it just shows the same as the first page. Any other ideas?

    <?php  global $paged; get_template_part( 'template-parts/header'); 
    if ( $total > 1 )  {
         // get the current page
         if ( !$current_page = get_query_var('paged') )
              $current_page = 1;
         // structure of "format" depends on whether we're using pretty permalinks
         if( get_option('permalink_structure') ) {
             $format = '?paged=%#%';
         }
         }
     ?>
<div class="blog-inner">
      <div class="grid-container">
        <div class="grid-x">
          <div class="large-8 column">
            <?php
              if ( get_query_var('paged') ) {
                 $paged = get_query_var('paged');
                } elseif ( get_query_var('page') ) {
                 $paged = get_query_var('page');
                } else {
                 $paged = 1;
                }

              $blogs = array(
                'post_type'      => 'post',
                'order'      => 'DESC',
                'posts_per_page' =>  3,
                'paged'      => $paged
              );

              $blog_posts = new WP_Query($blogs); ?>
            <?php if ( $blog_posts->have_posts() ) : while ( $blog_posts->have_posts() ) : $blog_posts->the_post(); ?>
              <div class="blog-post">
                <h4><?php the_title(); ?></h4>
                <div class="byline">

                <p><?php the_excerpt(); ?></p>

                  ?>
                </div>
                <div class="read-all">
                  <a href="<?php the_permalink();?>" class="">Read The Post</a>
                </div>
              </div>
            <?php endwhile; ?>
            <div class="pagination">
              <?php wp_reset_query(); ?>
                  <?php 
                      echo paginate_links( array(
                          'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                          'total'        => $blog_posts->max_num_pages,
                          'current'      => max( 1, get_query_var( 'paged' ) ),
                          'format'       => '?paged=%#%',
                          'show_all'     => false,
                          'type'         => 'plain',
                          'end_size'     => 2,
                          'mid_size'     => 1,
                          'prev_next'    => true,
                          'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                          'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                          'add_args'     => false,
                          'add_fragment' => '',
                      ) );
                  ?>
              </div>

            <?php endif; ?>
          </div>
                </div>
                <?php endwhile; ?>
              </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>

[some html-taken out, so some divs might not match up]

More Info: it is currently set as the index.php page, even though it's not the home page. if you get to it through misspelling a url, it works

  • Describe “not working” – Milo Oct 8 '18 at 15:47
  • @Milo when it goes to the second page, it shows the same pages as the first page – lukepnnngtn Oct 8 '18 at 16:38
0

Hello @lukepnnngtn and welcome! Prepare yourself for reading.

Since you're working on the index.php file we need to consider what's said in the docs under Removing query_posts from the Main Loop in the Pagination Codex (link):

For example, let's say your theme queries the main loop like this on your home page (index.php) and category pages (category.php) and the pagination is not working...

What you need to do is to remove the WP_Query() from the index.php file and make an action in the function.php. It's described beat by beat in the link but I will paste it here too:

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()){

    // alter the query for the home and category pages 

    if(is_home()){
      $query->set('posts_per_page', 3);
    }

    if(is_category()){
      $query->set('posts_per_page', 3);
    }

  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

After that we need to go back to the index.php file. At this point it should look something like this:

while ( have_posts() ) :
    the_post();

    ...

endwhile;

global $wp_query;

echo paginate_links( array(
    'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'         => $wp_query->max_num_pages,
    'current'       => max( 1, get_query_var('paged') ),
    'format'        => '?paged=%#%',
    'show_all'      => false,
    'type'          => 'plain',
    'end_size'      => 2,
    'mid_size'      => 1,
    'prev_next'     => true,
    'prev_text'     => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
    'next_text'     => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
    'add_args'      => false,
    'add_fragment'  => '',
) );

I looked into your code some more and I also suggest reading WordPress' documentation on (get_query_var()):

For getting the current pagination number on a static front page (Page template) you have to use the 'page' query variable...

After some more research, I found this answer (link) on another WP related question that reads:

  • paged -> Used on the homepage, blogpage, archive pages and pages to calculate pagination. 1st page is 0 and from there the number corresponds to the page number
  • page -> Use on a static front page and single pages for pagination. Pagination on these pages works the same, a static front page is treated as a single page on pagination.

This is straight from the docs (link):

If the pagination is broken on a static front page you have to add the "paged" parameter this way...

So what you did here is great:

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
}
elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
}
else {
    $paged = 1;
}

Your code was working just fine when I used it on a static page with a custom template up until the moment I made it Homepage, then you need to use the $paged variable for the current argument in the paginate_links() call, like this:

paginate_links( array(
    ...
    'current'       => max( 1, $paged ),
    ...
) );

I still can't ask questions in the comments so I hope I was helpful. Good luck!

| 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.