【发布时间】: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