第2页上的wordpress分页类别404



当我点击分页的下一页时,将我转移到page 404。我什么都试过了,但没有成功。我不确定是否需要在functions.php或URL中插入一些内容。这是我的代码:这是我的代码:

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

您需要将查询修改为:

$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'
    )
  );

对于分页,我这样更改了代码。希望这将帮助你:

$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
    ));

我组合了Gufran Hasan答案中的代码,并将此代码添加到我的函数php上。问题解决了!

谢谢,伙计!Gufran Hasan

代码:

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');

最新更新