tags:

views:

29

answers:

2

Hi

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

A: 

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.

Dennis Pedrie
Thanks Dennis - appreciate you taking the time. I have seen a lot of very complex solutions so a simple approach is gratefully received. Many thanks
ROb
A: 

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.

songdogtech
thanks - this is a great start that I can work on. Much appreciated.
ROb