Count Number Of Posts Per Category In Wordpress Theme
| Posted in Interesting | Posted on 28-01-2010
View Comments
After searching for days, to no avail, for a Wordpress plugin or hack to count the number of posts in a specific category I finally came up with a little coding of my own that should work in any Wordpress theme.
Copy/Paste the following code snippet into your theme’s functions.php file:(without the quotes)
“function number_postpercat($idcat) { global $wpdb; $query = “SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat”; $num = $wpdb->get_col($query); echo $num[0]; }“
You can edit the functions.php file by going to Appearance>>Editor in your Wordpress back-office.
You can then call the function by adding the following code snippet into your theme wherever you need to do the count:(without the quotes)
“<?php number_postpercat (4); ?>“
The result will be something like: Wordpress Themes(19) | Wordpress Plugins(21) etc.
The sample number “(4)” above is the Wordpress category ID.
I hope that this will save someone else some valuable time.

