【问题标题】:Wordpress return 404 when using query_posts with pagination使用带有分页的 query_posts 时,Wordpress 返回 404
【发布时间】:2014-10-13 19:35:08
【问题描述】:

我在我的类别模板上使用了一个带有 query_posts 的简单循环,并带有一个用于分页的自定义函数。第一页效果很好,但是当我使用下一步按钮进入下一页 (/page/2) 时,我收到 404 错误。

在我的 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>

生成的 URL 是正确的,并且在后台办公室中,每页的最大帖子数选项也设置为 6。我尝试在我的 functions.php 文件中使用该修复程序,但没有帮助:

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

【问题讨论】:

  • 这就是为什么你永远不应该使用query_posts,也是为什么你永远不应该使用自定义查询来替换任何类型的存档页面上的主查询

标签: php wordpress pagination


【解决方案1】:

这正是您永远不应该使用 query_posts 以及永远不应该使用自定义查询来替换任何类型存档页面上的主查询的原因

要解决您的问题,请返回应有的默认循环

if(have_posts()) {
   while(have_posts()) {
     the_post();

       //YOUR LOOP ELEMENTS

    }
}

然后,在您的 functions.php 中,使用 pre_get_posts 相应地更改主查询

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

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2011-03-15
    • 2013-11-07
    • 2012-08-31
    • 1970-01-01
    • 2021-10-27
    • 2017-05-23
    • 2018-04-03
    • 2014-09-20
    相关资源
    最近更新 更多