dongtan7351 2014-08-20 12:31
浏览 61
已采纳

使用带分页的query_posts时,Wordpress返回404

I'm using a simple loop with query_posts on my category template, with a custom function for pagination. First page works great, but when I to reach next page (/page/2) using the next button, I get a 404 error.

In my category.php :

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array ( 'category' => ID, 'posts_per_page' => 6, 'paged' => $paged);
$myposts = query_posts( $args );
if(have_posts()) :
      foreach( $myposts as $post ) :    setup_postdata($post);
      ?>    
      <article>
      ...
      </article>
      <?php
      endforeach;
else :
      echo '<p class="intro-contact">' . _e( 'No news available', 'cja_theme' ) . '</p>';
endif;
cja_numeric_posts_nav();
?>

.htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/www.mywebsite.com/

RewriteRule ^la-residence/$ la-residence/qui-sommes-nous/ [L,R=301]
RewriteRule ^informations/$ informations/acces-a-la-residence/ [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public/www.mywebsite.com/index.php [L]
</IfModule>

Generated URL is correct, and in the Back Office the max posts per page option is set to 6 too. I tried to use that fix in my functions.php file but it didn't help :

function cja_fix_pagination($query_string)
{
  if(!is_home())
    $query_string['posts_per_page'] = 6;
  return $query_string;
}
add_filter('request', 'cja_fix_pagination');
  • 写回答

1条回答 默认 最新

  • douxian0008 2014-08-20 12:50
    关注

    That is exactly why you should never ever use query_posts and also why you should never use a custom query to replace the main query on any type of archive page

    To solve your issue, return to the default loop as it should be

    if(have_posts()) {
       while(have_posts()) {
         the_post();
    
           //YOUR LOOP ELEMENTS
    
        }
    }
    

    Then, in your functions.php, use pre_get_posts to alter the main query accordingly

    function ppp_category( $query ) {
        if ( !is_admin && $query->is_category() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '6' );
        }
    }
    add_action( 'pre_get_posts', 'ppp_category' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成