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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?