要打印主主页查询的递减计数器,而不打印粘性帖子,您可以尝试:
// current page number - paged is 0 on the home page, we use 1 instead
$_current_page = is_paged() ? get_query_var( \'paged\', 1 ) : 1;
// posts per page
$_ppp = get_query_var( \'posts_per_page\', get_option( \'posts_per_page\' ) );
// current post index on the current page
$_current_post = $wp_query->current_post;
// total number of found posts
$_total_posts = $wp_query->found_posts;
// Decreasing counter
echo $counter = $_total_posts - ( $_current_page - 1 ) * $_ppp - $_current_post;
Example:
对于总共10篇文章,每页4篇文章,递减计数器应为:
Page 1:
10 - ( 1 - 1 ) * 4 - 0 = 10
10 - ( 1 - 1 ) * 4 - 1 = 9
10 - ( 1 - 1 ) * 4 - 2 = 8
10 - ( 1 - 1 ) * 4 - 3 = 7
Page 2:
10 - ( 2 - 1 ) * 4 - 0 = 6
10 - ( 2 - 1 ) * 4 - 1 = 5
10 - ( 2 - 1 ) * 4 - 2 = 4
10 - ( 2 - 1 ) * 4 - 3 = 3
Page 3:
10 - ( 3 - 1 ) * 4 - 0 = 2
10 - ( 3 - 1 ) * 4 - 1 = 1
或:
Page 1: 10,9,8,7
Page 2: 6,5,4,3
Page 3: 2,1
Update:
为了支持粘帖,我们可以通过以下方式调整上述计数器:
// Decreasing counter
echo $counter = $_total_posts
+ $sticky_offset
- ( $_current_page - 1 ) * $_ppp
- $_current_post;
我们定义:
$sticky_offset = is_home() && ! is_paged() && $_total_posts > $_ppp
? $wp_query->post_count - $_ppp
: 0;
请注意,粘帖可能有三种情况:
所有贴子都来自第一个(主页)。(主页上显示的帖子数量与没有粘性帖子的数量相同)
负1)混合1)和2)我们的调整应处理所有三种情况。