我有一个分类页,我得到一些有关分页的问题。当我点击下一页的时候
糟了!那一页找不到。

URL显示http://example.com/category/accessories/page/2/,这是正确的。
下面是我的完整代码,这是我正在使用的类别页面。
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.发布于 2019-11-16 17:19:16
类别档案默认使用此permalink/URL结构:
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中检索所使用的参数:
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?我打赌不会。
类别模板应该只显示通过主查询检索的帖子:
while ( have_posts() ) {
the_post();
// Display the post.
}但我在你的代码里看不到这一点。
您可以使用pre_get_posts修改主查询的参数:
// 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 );
}
} );在您的代码中,没有必要这样做:
$args = array('posts_per_page' => 6,'paged'=> $paged,);
$tyler_query = new WP_Query( $args ); // secondary query此外:
$tyler_query->have_posts()更改为have_posts()。$tyler_query->the_post()更改为the_post()。$tyler_query->max_num_pages )。https://wordpress.stackexchange.com/questions/352673
复制相似问题