Wordpress 分页 - 分页 returns 空页
Wordpress Pagination - Paged returns Empty Page
我有一个分页功能,可以正确显示页码,但是在点击下一页时,例如 2,它简单地显示一个白色的空白页,无论我做什么,我都尝试了所有可能的选项来至少让我看到错误,但什么也没有。我有一个分页功能,当我在短代码中使用它时,它工作得很好。但是在任何页面中,如果我在任何地方传递 page.php?page_id=1&paged=2 =2 在 GET 中,它显示空白页...
页面结构是这样的:
index.php 有以下代码..
get_header();
get_template_part( 'templates/_content' );
get_footer();
并且在 _content.php 模板文件中,我有以下代码..
if(is_home() OR is_category()) {
global $paged, $wp_query;
if(empty($paged)) $paged = 1;
$posts_per_page = 2;
$options = array(
'post_type' => 'post',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_status' => 'publish'
);
$wp_query = new WP_Query($options);
$pages = $wp_query->max_num_pages;
$custom_pagination = custom_pagination($pages,$posts_per_page);
if($wp_query->have_posts()) :
while($wp_query->have_posts()) : the_post();
get_template_part( 'templates/blog_archive_template' );
endwhile;
else:
echo '
<h2 class="center">Not Found !</h2>
<p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
endif;
echo $custom_pagination;
} else {
if(have_posts()) : while(have_posts()) : the_post();
/* rest of html code */
}
有人可以指出对我有帮助的一件事吗?谢谢你的时间。
问候
每页的默认帖子数为 10。您已在自定义查询中将其设置为 2,但此时 WordPress 已经确定不需要第 2 页,而是显示 404 模板。
您需要使用 pre_get_posts
修改主查询。
示例:
/**
* Modify the main query on the posts index or category
* page. Set posts per page to 2.
*
* @param object $query
*/
function wpse_modify_home_category_query( $query ) {
// Only apply to the main loop on the frontend.
if ( is_admin() || ! $query->is_main_query() {
return false;
}
// Check we're on a posts or category page.
if ( $query->is_home() || $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
add_action( 'pre_get_posts', 'wpse_modify_home_category_query' );
将此添加到 functions.php
后,从 index.php
模板中删除自定义查询并使用常规循环。
如果唯一改变的是每页的帖子数量并且它应该适用于全球,那么您最好在设置 -> 阅读页面上更改管理面板中的值。
我有一个分页功能,可以正确显示页码,但是在点击下一页时,例如 2,它简单地显示一个白色的空白页,无论我做什么,我都尝试了所有可能的选项来至少让我看到错误,但什么也没有。我有一个分页功能,当我在短代码中使用它时,它工作得很好。但是在任何页面中,如果我在任何地方传递 page.php?page_id=1&paged=2 =2 在 GET 中,它显示空白页...
页面结构是这样的:
index.php 有以下代码..
get_header();
get_template_part( 'templates/_content' );
get_footer();
并且在 _content.php 模板文件中,我有以下代码..
if(is_home() OR is_category()) {
global $paged, $wp_query;
if(empty($paged)) $paged = 1;
$posts_per_page = 2;
$options = array(
'post_type' => 'post',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_status' => 'publish'
);
$wp_query = new WP_Query($options);
$pages = $wp_query->max_num_pages;
$custom_pagination = custom_pagination($pages,$posts_per_page);
if($wp_query->have_posts()) :
while($wp_query->have_posts()) : the_post();
get_template_part( 'templates/blog_archive_template' );
endwhile;
else:
echo '
<h2 class="center">Not Found !</h2>
<p class="center">Sorry, but you are looking for something that isn\'t here.</p>';
endif;
echo $custom_pagination;
} else {
if(have_posts()) : while(have_posts()) : the_post();
/* rest of html code */
}
有人可以指出对我有帮助的一件事吗?谢谢你的时间。
问候
每页的默认帖子数为 10。您已在自定义查询中将其设置为 2,但此时 WordPress 已经确定不需要第 2 页,而是显示 404 模板。
您需要使用 pre_get_posts
修改主查询。
示例:
/**
* Modify the main query on the posts index or category
* page. Set posts per page to 2.
*
* @param object $query
*/
function wpse_modify_home_category_query( $query ) {
// Only apply to the main loop on the frontend.
if ( is_admin() || ! $query->is_main_query() {
return false;
}
// Check we're on a posts or category page.
if ( $query->is_home() || $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
add_action( 'pre_get_posts', 'wpse_modify_home_category_query' );
将此添加到 functions.php
后,从 index.php
模板中删除自定义查询并使用常规循环。
如果唯一改变的是每页的帖子数量并且它应该适用于全球,那么您最好在设置 -> 阅读页面上更改管理面板中的值。