admin管理员组文章数量:1316658
I know how to show the total number of posts in a site, but is there a way to show the number of posts the page you're on is displaying?
For example, I want to show this at the bottom of each of my blog pages (8 posts per page);
Index - "Showing posts 1 through 8 of 294 total"
Page2 - "Showing posts 9 through 17 of 294 total"
Page3 - "Showing posts 18 through 26 of 294 total"
The posts are displayed in chronological order if that makes a difference.
Is this even possible?
I know how to show the total number of posts in a site, but is there a way to show the number of posts the page you're on is displaying?
For example, I want to show this at the bottom of each of my blog pages (8 posts per page);
Index - "Showing posts 1 through 8 of 294 total"
Page2 - "Showing posts 9 through 17 of 294 total"
Page3 - "Showing posts 18 through 26 of 294 total"
The posts are displayed in chronological order if that makes a difference.
Is this even possible?
Share Improve this question asked Jul 26, 2014 at 20:29 Dean ElliottDean Elliott 1492 silver badges7 bronze badges3 Answers
Reset to default 2This should work, but I haven't tested it:
global $wp_query;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $page;
$start = $end - $ppp + 1;
$total = $wp_query->found_posts;
echo "Showing posts $start through $end of $total total.";
Because I'm doing that right now, I tested the last solution and it works except on the last page.
That's working now and tested:
global $wp_query;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $page;
$start = $end - $ppp + 1;
$total = $wp_query->found_posts;
if ($end > $total)
$end = $total;
@karpstrucking is really close but if the number of found posts is smaller than the number posts allowed per page, you'll echo a message like "Showing posts 1 through 12 of 9 total". To prevent this, the code should be updated to something like:
global $wp_query;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$total = $wp_query->found_posts;
$end = $ppp * $page;
$start = $end - $ppp + 1;
if( $ppp > $total ) {
$results_summary_html = '<p>Showing ' . $total . ' out of ' . $total;
} else if( $end > $total ) {
$results_summary_html = '<p>Showing ' . $start . '-' . $total . ' out of ' . $total;
} else {
$results_summary_html = '<p>Showing ' . $start . '-' . $end . ' out of ' . $total;
}
本文标签: paginationShow number of posts AND number on current page
版权声明:本文标题:pagination - Show number of posts AND number on current page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742001548a2411114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论