No plugin required!
When I first started using WordPress, it kind of bugged me that the only place I could see a count of posts, categories, and comments was in the Dashboard. I wanted that information available to site visitors, too.
So I rolled up my sleeves, took out my favorite text editor (TextWrangler), and began exploring WordPress template files.
I found what I was looking for right in the index.php file in the wp-admin folder. That’s the file that displays the Dashboard administration panel. I did a search for Blog Stats (the heading under which the information I wanted appeared). I found the following code:
<h3><?php _e('Blog Stats'); ?></h3>
<?php
$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);
?>
<p><?php printf(__('There are currently %1$s <a href="%2$s" title="Posts">posts</a> and %3$s <a href="%4$s" title="Comments">comments</a>, contained within %5$s <a href="%6$s" title="categories">categories</a>.'), $numposts, 'edit.php', $numcomms, 'edit-comments.php', $numcats, 'categories.php'); ?></p>
This code includes PHP and HTML to get and display the information I wanted, along with links to administrative pages. It would require some editing to make it display the way I wanted it to.
I copied all of this code and pasted it into the sidebar.php file for my theme in the place I wanted it to appear. Then I started editing to remove the links and change the way the information was presented. I ended up with this:
<h2><?php _e('Site Stats'); ?></h2>
<ul>
<li>
<?php $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);
?>
<?php printf(__('There are currently %1$s entries and %3$s comments, contained within %5$s blog categories.'), $numposts, 'edit.php', $numcomms, 'edit-comments.php', $numcats, 'categories.php'); ?></li>
</ul>
i saved the sidebar.php file to my theme folder and reloaded a page that included the sidebar. Voila! It worked!
You can see what it looks like in the Site Stats area in the sidebar of any page of this blog. You might notice the Users Online information there, too; that’s created with a plugin.












