admin管理员组文章数量:1421031
I'm trying to achieve the following search result counts while using pagination;
Showing 1-9 of 368 Items - (Page One)
Showing 9-18 of 368 Items - (Page Two)
but I'm getting myself turned around with $count_posts;
I'm not sure I'm fully grasping it. :/
PHP (Search.php)
<?php $post_count = $wp_query->post_count; ?>
<?php $count_products = wp_count_posts( 'products' )->publish; ?>
<?php $count_posts = wp_count_posts()->publish; ?>
HTML/PHP (Search.php)
<p class="small text-uppercase">Showing <?php echo $post_count; . '-' . $post_count; ?> of <strong><?php echo $count_products ?> items</strong></p>
My Question: How can I achieve the result I am after?
EDIT: 4 JUL 14:56
After quite a bit of testing I was able to figure it out with the help of, @KrzysiekDróżdż.
if ( !is_paged() ) {
// first page of pagination
$first_post = absint( $wp_query->get('paged') - 1 );
$last_post = $first_post + $wp_query->post_count - 1;
$all_posts = $wp_query->found_posts;
} else {
$first_post = absint( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
$last_post = $first_post + $wp_query->post_count - 1;
$all_posts = $wp_query->found_posts;
}
I'm trying to achieve the following search result counts while using pagination;
Showing 1-9 of 368 Items - (Page One)
Showing 9-18 of 368 Items - (Page Two)
but I'm getting myself turned around with $count_posts;
I'm not sure I'm fully grasping it. :/
PHP (Search.php)
<?php $post_count = $wp_query->post_count; ?>
<?php $count_products = wp_count_posts( 'products' )->publish; ?>
<?php $count_posts = wp_count_posts()->publish; ?>
HTML/PHP (Search.php)
<p class="small text-uppercase">Showing <?php echo $post_count; . '-' . $post_count; ?> of <strong><?php echo $count_products ?> items</strong></p>
My Question: How can I achieve the result I am after?
EDIT: 4 JUL 14:56
After quite a bit of testing I was able to figure it out with the help of, @KrzysiekDróżdż.
if ( !is_paged() ) {
// first page of pagination
$first_post = absint( $wp_query->get('paged') - 1 );
$last_post = $first_post + $wp_query->post_count - 1;
$all_posts = $wp_query->found_posts;
} else {
$first_post = absint( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
$last_post = $first_post + $wp_query->post_count - 1;
$all_posts = $wp_query->found_posts;
}
Share
Improve this question
edited Jun 15, 2020 at 8:21
CommunityBot
1
asked Jun 28, 2019 at 10:18
LewisLewis
4083 silver badges16 bronze badges
1 Answer
Reset to default 2I'm afraid you're doing it a little bit wrong...
Here are the problems:
post_count
field contains the number of posts being displayed and not the offset.wp_count_posts
returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).echo $post_count; . '-' . $post_count;
is not correct PHP code.
So how to do this?
All you need is the info from WP_Query:
<?php
$first_post = absint( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
$last_post = $first_post + $wp_query->post_count;
$all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>
本文标签: phpCounting Search resultsand displaying the offset per page
版权声明:本文标题:php - Counting Search results, and displaying the offset per page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745335675a2654026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论