Here’s a quick way to get and show some useful stats on your wordpress blog. This will show the number of posts,comments and categories you have to allow your visitors and yourself a broad view of your blog’s current state updated as these criteria change. By the end of this how-to, you would be displaying something like this:

So let’s start. Open either sidebar.php or footer.php from your current Wordpress theme’s folder depending on whether you want to display your quick stats in the sidebar thus sidebar.php or footer then footer.php. At the beginning of your sidebar/footer.php copy and paste the following lines of code :
<?php /* Counts the posts, comments and categories on your blog */
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
$numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
if (0 < $numcats) $numcats = number_format($numcats);
?>
Then move along the same template file - sidebar.php/footer.php and spot the place where you want your quick shooting stats to get displayed. Then copy and paste these lines where you want them to appear for your great audience.
There are currently <?php echo $numposts; ?> posts extending over <?php echo $numcats; ?> categories with loads upto <?php echo $numcomms; ?> comments.
That’s it all. Get to your blog and see your great stats being displayed. Feel free to share this tip or post your comments if you need any clarification or help which I would be glad to send in your way.













thnks hans!
keep it up!
i added it on my blog
This is great, but you might want to put the first example code in the <code> tag and not a blockquote, and replace quotes and apostrophes with character entities to avoid the questions you are going to get to fix this because it won’t work when people copy and paste, as you have told them to do.
Tips on how to do this are in this helpful article on putting code in WordPress posts.
Thanks for doing this. It’s really great.
@ Yashvin
nice to know it works… more check this out stuffs on the way
stay tuned….
@ Lorelle
thanks very much for your comment and the link…. I just put it that way since each time it displayed only part of the code when using the code tag.. seems like your article will be of great help indeed to me.
Have you tried the above trick on wordpress.com?
Hans, i thought you might want to know, me and kyu have tested this under very harsh conditione (You know, when we dissecated colourful wall.) The post count part does not exclude the [ages on your site.
oops, [ages = pages
@ leo:
yeah perhaps wordpress consider them the same
hey you could have asked me and I would have sent it your way instead of you breaking all this ice to find some gold lol
thanks for trying it
more in the directory to come
for your great hackers at kyusphere… let’s see if you can figure out how I did the stuff for my category, that’s displaying and rotating 3 posts under each of its category
Thanks for this, Hans.
I’ve just added it to my site, and although there are actually only 194 posts, it says there are 200. Perhaps it counts pages too?
@ David
yes the code counts pages too
btw you could have a look at this one too allowing you to display your blogging age. Thanks for your visit and your comment.
Nice Site!
Nice trick, thanks. I’m looking for a way to show how many visitors are there at any given time on the blog, you know a way?