开发者

Returning number of posts in Wordpress

开发者 https://www.devze.com 2023-01-08 11:10 出处:网络
I need a function to calculate the number of posts in a Wordpress blog that is aware if you are looking at a category, a given tag or the whole blog.

I need a function to calculate the number of posts in a Wordpress blog that is aware if you are looking at a category, a given tag or the whole blog.

I'm keen to avoid rewriting for every different circumstance and want to make sure I get off on a reliable path. Relatively 开发者_运维问答new to Wordpress any help appreciated.

Thanks


If you want to retrieve a count of all the posts in a blog, use wp_count_posts(). To get post counts from a specific category, do a count() on a call to get_posts() with the category ID specified as a parameter.

Example:

<?php
$posts = get_posts('category=1');
$count = count($posts);

echo $count;
?>

Unfortunately WordPress' wp_count_posts() function won't count a category's posts. It'll only count different post types, i.e posts, pages, drafts, and in 3.0, custom post types.


Provides a template function: WordPress › Count Posts « WordPress Plugins. Could borrow the code from it and integrate it into your theme's functions.php file.

0

精彩评论

暂无评论...
验证码 换一张
取 消