admin管理员组文章数量:1130349
Building the category.php I'm curious to know if there is a better way to get the count of posts without wp_query? In the past I've called:
single_cat_title()to render the categorycategory_description()to render the category description
and to build a separator after each post but not the last on the page or last in get_option('posts_per_page') I've used:
$category = get_queried_object();
$category_total = $category->count;
From research I did see How to get Total Post In a Selected category which was authored in 2011. wp_count_posts() counts all posts in a blog which I dont think would be good on performance.
$category = get_category($id);
$count = $category->category_count;
From Count how many posts in category I think requires wp_query? Is there a better way to get the count of posts in a category for category.php?
Building the category.php I'm curious to know if there is a better way to get the count of posts without wp_query? In the past I've called:
single_cat_title()to render the categorycategory_description()to render the category description
and to build a separator after each post but not the last on the page or last in get_option('posts_per_page') I've used:
$category = get_queried_object();
$category_total = $category->count;
From research I did see How to get Total Post In a Selected category which was authored in 2011. wp_count_posts() counts all posts in a blog which I dont think would be good on performance.
$category = get_category($id);
$count = $category->category_count;
From Count how many posts in category I think requires wp_query? Is there a better way to get the count of posts in a category for category.php?
- What count do you actually need? The total number of posts in a category, or the number of posts on the current page? – Jacob Peattie Commented Dec 18, 2018 at 3:38
- both actually to render current design which is why I'm also pulling 'posts_per_page'. – user9447 Commented Dec 18, 2018 at 3:39
1 Answer
Reset to default 1What you've got is basically correct. This is the correct method for getting the total number of posts in the currnet category:
$category = get_queried_object();
$category_total = $category->count;
There shouldn't be any performance impact from using this, as the count property is only updated when new posts are published, and doesn't need to be calculated each time it's used.
To get the number of posts on the current page though, get_option( 'posts_per_page' ) is not the best option, because it will be the incorrect number on the last page, which could have less than the total number of posts per page.
To get that number you need to check the global $wp_query object:
global $wp_query;
$post_count = $wp_query->post_count;
Building the category.php I'm curious to know if there is a better way to get the count of posts without wp_query? In the past I've called:
single_cat_title()to render the categorycategory_description()to render the category description
and to build a separator after each post but not the last on the page or last in get_option('posts_per_page') I've used:
$category = get_queried_object();
$category_total = $category->count;
From research I did see How to get Total Post In a Selected category which was authored in 2011. wp_count_posts() counts all posts in a blog which I dont think would be good on performance.
$category = get_category($id);
$count = $category->category_count;
From Count how many posts in category I think requires wp_query? Is there a better way to get the count of posts in a category for category.php?
Building the category.php I'm curious to know if there is a better way to get the count of posts without wp_query? In the past I've called:
single_cat_title()to render the categorycategory_description()to render the category description
and to build a separator after each post but not the last on the page or last in get_option('posts_per_page') I've used:
$category = get_queried_object();
$category_total = $category->count;
From research I did see How to get Total Post In a Selected category which was authored in 2011. wp_count_posts() counts all posts in a blog which I dont think would be good on performance.
$category = get_category($id);
$count = $category->category_count;
From Count how many posts in category I think requires wp_query? Is there a better way to get the count of posts in a category for category.php?
- What count do you actually need? The total number of posts in a category, or the number of posts on the current page? – Jacob Peattie Commented Dec 18, 2018 at 3:38
- both actually to render current design which is why I'm also pulling 'posts_per_page'. – user9447 Commented Dec 18, 2018 at 3:39
1 Answer
Reset to default 1What you've got is basically correct. This is the correct method for getting the total number of posts in the currnet category:
$category = get_queried_object();
$category_total = $category->count;
There shouldn't be any performance impact from using this, as the count property is only updated when new posts are published, and doesn't need to be calculated each time it's used.
To get the number of posts on the current page though, get_option( 'posts_per_page' ) is not the best option, because it will be the incorrect number on the last page, which could have less than the total number of posts per page.
To get that number you need to check the global $wp_query object:
global $wp_query;
$post_count = $wp_query->post_count;
本文标签: categoriesIs there a cleaner way to get post count for a category in categoryphp
版权声明:本文标题:categories - Is there a cleaner way to get post count for a category in category.php? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749089059a2314232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论