第2页上的WordPress分页类别404

时间:2018-11-14 15:29:30

标签: php html wordpress

当我单击分页的下一页时,将我转移到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>

2 个答案:

答案 0 :(得分:1)

您需要将查询修改为:

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

答案 1 :(得分:0)

我结合了答案Gufran Hasan的代码,并将此代码添加到我的 function.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');