单击分页选项卡时找不到页面

时间:2019-11-16 作者:Naren Verma

我有一个分类页面,我在分页上遇到了一些问题。当我点击下一页时

哎呀!找不到该页。

enter image description here

URL正在显示http://example.com/category/accessories/page/2/ 这是正确的。

下面是我在分类页面上使用的完整代码。

<?php
   /**
   * A Simple Category Template
   */
get_header(); 
?>  
<div id="primary" class="content-area">
   <main id="main" class="site-main">
      <div class="container">
         <div class="equalPadding"> 
   <?php 
  global $paged;
  $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
  $args = array(\'posts_per_page\' => 6,\'paged\'=> $paged,); 
  $tyler_query = new WP_Query( $args );
       if ($tyler_query->have_posts()){
          if(is_category()){          
                 $category_images = get_option( \'category_images\' );
                 $category_image = \'\';
                 if ( is_array( $category_images ) && array_key_exists( get_query_var(\'cat\'), $category_images ) ){
                 $category_image = $category_images[get_query_var(\'cat\')] ;
                 ?>        
            <div class="category-img" style="background-image:url(<?php echo $category_image;?>)">
            <div class="category_heading d-flex related-post">
            <div class="cp-heading"><h1><?php single_cat_title(\'\'); ?></h1></div>
        <?php   //check if category is a subcategory

    if( 0 != $this_category->parent ){ //if subcategory then display feature post
         //displaying feature post here
    $args = array(\'posts_per_page\' => 3,\'meta_key\' => \'_featured-post\',\'meta_value\' => 1);
    $featured = new WP_Query($args);
    if ($featured->have_posts()):?><div class="subCat_featurePost post-list ">
            <?php while($featured->have_posts()): $featured->the_post();?>
            <div class="item"> <div class="thumb"><a href="<?php esc_url(the_permalink()); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail();?></a></div>
             <div class="title"><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute(\'echo=0\'));?>"><?php echo wp_trim_words(get_the_title(), 16, \'...\');?></a></div></div>
              <?php endwhile;?></div><?php else:
    endif;
    }?>
    </div>
  </div>
  <?php } } ?>
      <div class="categoryWrapper_list cp-seeWrapper">
            <div class="row">
       <?php $index = 0;
            $check=0;
//Main category list displaying from here
        while ($tyler_query->have_posts()){
           $tyler_query->the_post();
     if ($index < 4) {?>
        <div class="col-xl-12 col-lg-2 col-md-12 col-sm-12 col-xs-12">  
        <div class="row spaceBottom cp-shadow bg-white cp-homeRelated">
        <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 padding0">
        <div class="cp-homeRelatedImg"><a href="<?php esc_url(the_permalink()); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail();?></a>
       </div>
          </div>
    <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12 colcentertext">
        <div class="marginautoWrapper">
        <?php $category = get_queried_object();
              if ( ! empty( $category ) ) {
              $cat_list=\'<a href="\' . esc_url( get_category_link( $category->term_id ) ) . \'">\' . $category->name . \'</a>\';
               }?>
        <div class="d-flex cp-CategoryList">
                <div class="seeDate"><?php echo get_the_date(\'F j, Y\');?></div>
                <div class="cp_cat_list"><?php echo $cat_list;?></div>
              </div>
          <div class="cp-b-content">
            <h2><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute(\'echo=0\'));?>"><?php echo wp_trim_words(get_the_title(), 16, \'...\');?></a></h2>
<p><?php echo wp_trim_words(get_the_excerpt(), 35, \'...\');?></p>
            <h5>By <span><?php the_author(); ?></span></h5>
    </div>
    </div>
  </div>  
  </div>
    </div>
      <?php  } else {
        if ($check==0){?>
         <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12 padding0"><div class="row">
      <?php $check=1;}?>
        <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
          <div class="cp-shadow cp-seeSinglePostWrapper">
            <a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute(\'echo=0\'));?>" class=""><?php echo get_the_post_thumbnail();?></a>
            <div class="bg-white single-post-box">
              <div class="d-flex cp-CategoryList">
                <div class="seeDate"><?php echo get_the_date(\'F j, Y\');?></div>
                <div class="cp_cat_list"><?php echo $cat_list;?></div>

              </div>
              <div class="cp-b-content">
                <h2><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute(\'echo=0\'));?>" class=""><?php echo wp_trim_words(get_the_title(), 12, \'...\');?></a>
                </h2>
                  <p><?php echo wp_trim_words(get_the_excerpt(), 25, \'...\');?></p>
              <h5>By <span><?php the_author(); ?></span></h5>
                </div>
            </div>
            </div>
        </div>
           <?php   } $index++;}?></div></div>

  <?php if ($index > 4) {?>
  <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <div class="cp-social cp-shadow bg-white">
        <h3>Social</h3>
           <?php if (is_active_sidebar(\'footer-social\')) :
                 dynamic_sidebar(\'footer-social\');
                endif;
      ?>
  </div>
    <div class="cp-newsletter cp-shadow bg-white">
         <?php  if (is_active_sidebar(\'cp_newsletter\')) :
                 dynamic_sidebar(\'cp_newsletter\');
                endif;?>
    </div>
</div>
       <?php } ?>
  <div class="cp-postPagination">
     <div class="nav-previous cp-pagi float-left"><?php previous_posts_link(); ?></div>
       <div class="nav-next cp-pagi float-right"><?php next_posts_link( null, $tyler_query->max_num_pages ); ?> </div>
      <div class="clearfix"></div>
  </div>

  </div>
</div>
    <?php } else{?><p>Sorry, no posts matched your criteria.</p><?php } ?>


         </div>
      </div>
   </main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

第一件事:

类别存档默认使用此永久链接/URL结构:

http://example.com/category/<category slug>/ (for page #1)
http://example.com/category/<category slug>/page/<page number>/ (for page #2, #3, and so on)
所以当你访问http://example.com/category/accessories/page/2/, WordPress将自动呼叫WP_Query 使用的参数从当前页面URL检索:

WP_Query( array(
  \'category_name\' => \'accessories\', // the <category slug> in the URL
  \'paged\'         => 2,             // the <page number> in the URL
) );
这是主要的查询。

So when the paged value is greater than the max number of pages for the main query, then a 404 error page would be displayed. 在你的情况下accessories 是否确实有足够数量的文章供存档页面使用,以获得第#2页?我打赌没有。

第二件事:

类别模板应该只显示通过主查询检索到的帖子:

while ( have_posts() ) {
    the_post();
    // Display the post.
}
但我在你的代码中没有看到这一点。

第三件事:

您可以使用pre_get_posts 要通过修改其参数来更改主查询,请执行以下操作:

// This should go in your theme\'s functions.php file.
add_action( \'pre_get_posts\', function ( $query ) {
    // Set the number of posts per page to 6, but only on category archives.
    if ( ! $query->is_admin && $query->is_main_query() && is_category() ) {
        $query->set( \'posts_per_page\', 6 );
    }
} );
那么在您的代码中,没有必要这样做:

$args = array(\'posts_per_page\' => 6,\'paged\'=> $paged,);
$tyler_query = new WP_Query( $args ); // secondary query
此外:

更改$tyler_query->have_posts()have_posts().

然后更改$tyler_query->the_post()the_post().

只需使用<?php next_posts_link(); ?> 无需通过最大页数($tyler_query->max_num_pages 在代码中)。

相关推荐

突出显示第一页Pagination_Links

我需要WordPress的帮助。我不是WordPress开发人员,但出于某种原因,我必须在这里更改一些代码。现在说到点子上。我有一个页面,显示一些分页内容。Im使用paginate_links() 生成分页。当我在第一页时,页码不会突出显示它。但从第二页开始,它会突出显示页码。但我也要强调第一点。以下是我使用的代码:<?php $args = array( \'base\' => \'%_%\', \'format\' => \'#page%#%\