Maria’s Guides

Support and additional material for readers of books, articles, and digital media by Maria Langer.


Random Book Cover #1Random Book Cover #2Random Book Cover #3Random Book Cover #4Random Book Cover #5Random Book Cover #6Random Book Cover #7Random Book Cover #8Random Book Cover #9

Easy WordPress Site Stats

Posted on May 2nd, 2006 at 1:35 pm ·
Filed in: RSS WordPress Books   

No plugin required!

ActivityWhen 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.

 • Read 165 Times
Add to Del.icio.usAdd to Del.icio.us • Technorati ThisTechnorati This • Digg ThisDigg This • Stumble ItStumble it! • Twit ThisTwit This


 

 

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

If you have a Gravatar, it will appear beside your approved comment.
No Gravatar? Get one free!