简体   繁体   中英

wordpress pagination category 404 on page 2

When I click on the next page of my pagination, transfer me to page 404 . I tried everything, but there was no success. I'm not sure if I need to insert something into functions.php or in a URL. Here's my code: Here's my code:

<ul>
    <?php
     $paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
    $monthnum = (get_query_var('monthnum')) ? absint(get_query_var('monthnum')) : -1;
    $year = (get_query_var('year')) ? absint(get_query_var('year')) : -1;

    $args = array(
          'post_type'      => 'post',
           'posts_per_page' => 5,
           'taxonomy'       => 'category',
           'field'          => 'slug',
           'paged'          => $paged,
        );
       if($monthnum != -1 && $year != -1){
        $args["monthnum"] = $monthnum;
        $args["year"] = $year;
       }
      $posts = new WP_Query($args);
      if ($posts->have_posts()) {
         while ($posts->have_posts()) {
            $posts->the_post();
     ?>
   <li>
    <span class="date"><?php echo get_the_time('d.m.y') ?></span>
      <a href="<?php echo get_the_permalink(); ?>"><h3><?php the_title() ?></h3></a>
    <div class="postContent">
      <!-- start postContent -->
      <div class="postExcerpt"><?php the_excerpt(); ?></div>
      <div class="permalink"><a href="<?php echo get_the_permalink(); ?>">Read More</a></div>
      </div>
   <!-- end postContent -->
  </li>
 <?php } ?>
   <div class="pagination">
   <!-- start pagination-->
  <?php $big = 999999999; // need an unlikely integer
    echo paginate_links(array(
            'base'  => str_replace($big, '%#%',esc_url(get_pagenum_link($big))),
            'format'    => '/paged/%#%',
            'current'   => max(1, $paged),
            'total'     => $posts->max_num_pages,
            'mid_size'  => 1,
            'prev_text' => "<",
            'next_text' => ">"
         )); 
?> 
     </div>
    <?php   
      } else {
                echo 'No matching results found. Please modify your 
                   search criteria and try searching again.!';
           }
       ?>

      <!-- end pagination-->
    </ul>

You need to modify query as:

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

For pagination, I changed this code like this. Hope this will help you:

$big = 999999999;  
echo paginate_links(array(
          'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
           'format' => '/page/%#%',
            'current' => max(1, $paged),
            'prev_text' => __('Previous Page'),
            'next_text' => __('Next Page'),
            'show_all' => true,
            'total' => $query->max_num_pages
    ));

I combined the code from the answer Gufran Hasan, and add this code on my function.php . Problem solved!

Thanks man! Gufran Hasan

Code:

function custom_pre_get_posts($query)
{
   if ($query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {
    $query->set('page_val', get_query_var('paged'));
    $query->set('paged', 0);
   }
}

add_action('pre_get_posts', 'custom_pre_get_posts');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM