How can I use paginate_links without getting a 404 error in wordpress?

Viewed 156

I'm trying to build a correct pagination for a custom post type archive and the only function that seems to work in this case is paginate_links(). The problem is that when I click "Older posts" or second page I get a 404 error. I have a custom loop with wp_query. I can't understand where the problem can be, I already tried changing permalinks but it didn't work. Anybody have any idea of how to fix this? Thank you in advance guys!

<?php
      $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
      $query = new WP_Query(array(
              'post_type' => 'post',
              'posts_per_page' => 6,
              'orderby' => 'date',
              'order' => 'DESC',
              'paged' => $paged
      ));
?>

<div class="container">




<div class="row servizi">
  <?php if ($query -> have_posts()) : ?>
  <?php while($query -> have_posts()) : $query -> the_post(); ?>

    <div class="col-lg-4">
      <div class="ratio-box">
        <div class="inner-ratio-box">
          <?php if(has_post_thumbnail()) : ?>
            <a href="<?php the_permalink(); ?>" rel="nofollow">
              <?php the_post_thumbnail('large', array('class' => 'responsive inner-img')); ?>
            </a>
          <?php endif; ?>
        </div>
      </div>
      <p><a href="<?php the_permalink(); ?>"><?php the_title();?></a>
      <br><a href="<?php echo esc_url( $category_link ); ?>" title="Category Name"><?php the_category(', '); ?></a>
      <br><?php $archive_year  = get_the_time('Y');
                $archive_month = get_the_time('m');
                $archive_day   = get_the_time('d'); ?>
                <a href="<?php echo get_day_link( $archive_year, $archive_month, $archive_day); ?>"> <?php the_date('d/m/Y'); ?></a></p>
    </div>

  <?php endwhile; ?>
  <?php // next_posts_link() usage with max_num_pages.
  echo paginate_links( array(
          'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
          'total'        => $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' => '',
      ) );
      // Clean up after the query and pagination.
      wp_reset_postdata(); ?>


  <?php endif; ?>


</div>
0 Answers
Related