首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击分页选项卡时找不到

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

WordPress Development用户
提问于 2019-11-16 09:51:55
回答 1查看 283关注 0票数 1

我有一个分类页,我得到一些有关分页的问题。当我点击下一页的时候

糟了!那一页找不到。

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

下面是我的完整代码,这是我正在使用的类别页面。

代码语言:javascript
复制
    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')] ;
                 ?>        
            
            
            
        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()):?>
            have_posts()): $featured->the_post();?>
             
             
              
    
  
  
      
            
       have_posts()){
           $tyler_query->the_post();
     if ($index < 4) {?>
          
        
        
        
       
          
    
        
        term_id ) ) . '">' . $category->name . '';
               }?>
        
                
                
              
          
            

            By 
    
    
    
  
    
      
         
      
        
          
            
            
              
                
                

              
              
                
                
                  
              By 
                
            
            
        
           

   4) {?>
  
        
        Social
           
  
    
         
    

       
  
     
       max_num_pages ); ?> 
      
  

  

    Sorry, no posts matched your criteria.
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-11-16 17:19:16

第一件事:

类别档案默认使用此permalink/URL结构:

代码语言:javascript
复制
http://example.com/category// (for page #1)
http://example.com/category//page// (for page #2, #3, and so on)

因此,当您访问http://example.com/category/accessories/page/2/时,WordPress将自动调用WP_Query并从当前页面URL中检索所使用的参数:

代码语言:javascript
复制
WP_Query( array(
  'category_name' => 'accessories', // the  in the URL
  'paged'         => 2,             // the  in the URL
) );

这是主要的查询。

So当 paged value the主查询 the主查询<#>的最大页面数时,将显示 404 error页。在您的示例中,类别accessories是否实际有足够的帖子数量使存档页具有一个页面#2?我打赌不会。

第二件事:

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

代码语言:javascript
复制
while ( have_posts() ) {
    the_post();
    // Display the post.
}

但我在你的代码里看不到这一点。

第三件事:

您可以使用pre_get_posts修改主查询的参数:

代码语言:javascript
复制
// 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 );
    }
} );

在您的代码中,没有必要这样做:

代码语言:javascript
复制
$args = array('posts_per_page' => 6,'paged'=> $paged,);
$tyler_query = new WP_Query( $args ); // secondary query

此外:

  1. $tyler_query->have_posts()更改为have_posts()
  2. 然后将$tyler_query->the_post()更改为the_post()
  3. 只需使用,而不必传递最大页数(代码中的$tyler_query->max_num_pages )。
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/352673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档