第一页的offset)是非负整数。对于paged=1
偏移量为offset_fp
, 否则就是offset_op
对于其他页面。
Example #1:
首先,我们计算几页的偏移量,以便更好地理解这一点:For paged=1:
offset_fp = 0
For paged=2:
offset_op = (2-2)*10 + 13 + 0
= 13
For paged=3:
offset_op = (3-2)*10 + 13 + 0
= 10+13
= 23
...
以下是每页的帖子索引列表:0,1,2,3,4,5,6,7,8,9,10,11,12 (offset_fp=0, pp_fp=13, paged=1)
13,14,15,16,17,18,19,20,21,22 (offset_op=13, pp_op=10, paged=2)
23,24,25,26, 27,28,29,30,31,32 (offset_op=23, pp_op=10, paged=3)
...
我们可以看到偏移量与索引匹配。Example #2:
让我们采取行动pp_fp = 3
, pp_op = 5
, offset_fp=4
并计算偏移量op:For paged=1:
offset_fp = 4
For paged=2:
offset_op = (2-2)*5 + 3 + 4
= 7
For paged=3:
offset_op = (3-2)*5 + 3 + 4
= 5+3+4
= 12
...
并将其与指数进行比较:4,5,6 (offset_fp=4, pp_fp=3, paged=1)
7,8,9,10,11 (offset_op=7, pp_op=5, paged=2)
12,13,14,15,16 (offset_op=12, pp_op=5, paged=3)
...
Demo Plugin
下面是一个演示实现:/**
* Plugin Name: WPSE demo
*/
add_action( \'pre_get_posts\', function( \\WP_Query $query )
{
// Nothing to do if backend or not home page or not the main query
if ( is_admin() || ! $query->is_home() || ! $query->is_main_query() )
return;
// Get current pagination
$paged = get_query_var( \'paged\', 1 );
// Modify sticky posts display
$query->set( \'ignore_sticky_posts\', true );
// Modify post status
$query->set( \'post_status\', \'publish\' );
// Edit to your needs
$pp_fp = 13; // posts per first page
$pp_op = 10; // posts per other pages
$offset_fp = 0; // offset for the first page
// Offset for other pages than the first page
$offset_op = ( $paged - 2 ) * $pp_op + $pp_fp + $offset_fp;
// Modify offset
$query->set( \'offset\', $query->is_paged() ? $offset_op : $offset_fp );
// Modify posts per page
$query->set( \'posts_per_page\', $query->is_paged() ? $pp_op : $pp_fp );
} );
希望您能根据自己的需要进行调整!