我试图只在家里显示最后10个帖子,而不创建/page/2、/page/3,存档。
我一直在测试,如果您禁用分页,它只会抓取所有崩溃的服务器。(不要在家里这样做)。
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 10 );
$query->set( 'nopaging' , true );
}有人建议“no_found_rows=true”也做不到。
难道这是不可能的吗?看起来它要么创建页面,要么显示所有,没有办法“限制”它?
发布于 2019-02-09 20:42:26
这是一个错误,它将检索到的帖子:
$query->set( 'nopaging' , true );相反,你应该做的是:
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 10 );
$query->set( 'paged', '1'); // Makes /page/2/, etc links redirect to home
$query->set( 'no_found_rows', true ); // Avoid counting rows, faster processing.
}如果主题仍然呈现导航按钮,则必须添加一些逻辑将其隐藏在禁用分页的页面上,对我来说,这是主页:
if (!is_home()) {
// show pagination buttons
}发布于 2018-10-03 21:24:28
如果要阻止API生成分页链接,可以使用found_posts筛选器使WordPress认为从当前查询返回的帖子不会超过10个。
add_filter( 'found_posts', 'wpd_disable_home_pagination', 10, 2 );
function wpd_disable_home_pagination( $found_posts, $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() && $found_posts > 10 ) {
return 10;
}
return $found_posts;
}编辑-
您可以重定向任何分页URL:
add_action( 'pre_get_posts', 'wpd_redirect_pagination_urls', 10, 2 );
function wpd_redirect_pagination_urls( $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() && $query->is_paged() ) {
wp_redirect( get_post_type_archive_link( 'post' ) );
exit;
}
}发布于 2018-10-03 21:10:19
nopaging参数用于显示所有帖子或使用分页(https://codex.wordpress.org/Class_参考/可湿性粉剂_Query#Pagination_参数)。默认值是'false':使用分页。所以,当你把它设定为真的时候,它是预期的行为,天就塌下来了(如果你有大量的帖子)。
通过只使用posts_per_page参数,查询将获取您告诉它的帖子数量,这就是。
https://wordpress.stackexchange.com/questions/315850
复制相似问题