不同的posts_per_page取决于页码 [英] Different posts_per_page depending on page number
本文介绍了不同的posts_per_page取决于页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的CODE A"用于排除类别.但我也想做到这一点:
"CODE A" below is used to exclude categories. But I also want to accomplish this:
如果是第 1-3 页,则显示 8 个帖子
如果是第 4 页或更多,则显示 9 个帖子
If is page 1-3, display 8 posts
If is page 4 or more, display 9 posts
如何实现?以及如何将其添加到代码 A"中?
How to accomplish this? And how can this be added into "CODE A" ?
.
代码 A
function exclude_categories($query){
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array(60, 61) );
}
}
add_action( 'pre_get_posts', 'exclude_categories' );
推荐答案
我还不能测试它,但这可以工作
I'm not able to test it yet, but this could work
function exclude_categories($query){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( $paged < 4 ) :
$query->set( 'posts_per_page', 8 );
else :
$query->set( 'posts_per_page', 9 );
endif;
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array(60, 61) );
}
}
add_action( 'pre_get_posts', 'exclude_categories' );
也许你必须更换
Perhaps you have to replace
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
与
global $paged;
$paged = ( is_int($paged) && $paged !== 0 ) ? $paged : 1;
这篇关于不同的posts_per_page取决于页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文