^^ Yea I want to be able to do that too-i cant figure out how for the life of me. 🙁 any help would be appreciated!!! thanks everyone.
Hmm. I thought I had seen a hack/plugin that does this, but can’t come up with it. Anyway, it’s a (ok, relatively) simple bit of code you can place in your page template or my-hacks.php:
<?php
if($cat)
$postsincat = count($wpdb->get_col("SELECT post_id FROM $tablepost2cat WHERE category_id = $cat"));
?>
Then to display it:
<?php echo $postsincat; ?>
This is very helpful, but I’ve got a questio expanding on it. When viewing a single post, I’d like to be able to display anywhere on the page something like:
“You are reading post X of Y total posts in this category”
The code you’ve written above should help me substitute the total number of posts for the “Y” above (thanks!), but how can I get the “X” to display the number of the current post you’re reading?
Thanks for any ideas you have.
Make it a function (to be included within the ‘function.php’ in your theme folder, for example):
function number_postpercat ($idcat, $post_status='publish') {
global $wpdb;
$result=$wpdb->get_col("SELECT c.post_id FROM {$wpdb->post2cat} c INNER JOIN {$wpdb->posts} p ON c.post_id = p.id WHERE c.category_id = $idcat AND p.post_status='$post_status'");
$number=count($result);
echo $number;
}
Then trigger it including the following in the appropiate place of your template:
<?php number_postpercat (4); ?>
…where ‘4’ is the required category id.
Have a good day,
hip