statspage1

Hello guys! For my first guest post here at Pro Blog Design, I’m going to show you how to sell more ad spaces by adding stats to your WordPress blog’s Advertise page.

Unless stated, the PHP codes here are going to go into your content itself. That means youl have to use WordPress Page Templates (They allow you to make a theme file specifically for your Advertise page).

The Blog Herald has a good guide for this. They’re simple enough to use, so don’t be put off!

Displaying the Total Number of Posts

A large amount of posts means lots of content of course, but also many search engines visitors and regular blog updates. The kind of stuff advertisers totally enjoy.

We have to use the $wpdb object and the get_var() method to get the number of posts:

1
2
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

Then, you’ll have a $numposts variable containing the total number of posts. You can use it that way:

1
<?php echo $numposts.' has been published since January 12, 2008'; ?>

To see this function in action, visit WpRecipes archive page.

Displaying the Total Number of Comments

Are your blog posts are commented a lot? If yes, you should totally display the total number of comments written since your blog launch.

The following code works exactly the same than the one I’ve used to get the total number of posts:

1
2
3
4
<?php
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
?>

Once you have a $numcomms variable containing the total number of comments, you can print it on the screen:

1
<?php echo "There have been ".$numcomms." comments on this blog."; ?>

Displaying the Average Number of Comments Per Post

Now that we have the total numbers of posts and the total number of comments stored in two variables, we can easily work out the average number of comments per post (And round it off to a whole number).

If it’s high, it shows that your readers have a lot of interaction with your blog.

1
2
3
<?php
$avcomms = round($numposts/$numcomms);
?>

And to display that in your page, you use:

1
<?php echo "There is an average of ".$avcomms." comments per post."; ?>

Displaying the Total Number of Trackbacks

On the previous hack, you probably noticed that I excluded trackbacks from the comment count. In my opinion, the trackback count should be displayed separately from comments to show how many blog owner found your posts so interesting that they cited it in their own posts.

For displaying the total number of trackbacks, I have chosen to create a function instead of using the code directly as I did with the two previous functions. That way, you can choose which solution you find the best.

Paste this function on the functions.php file (Create one if needs be) from your theme:

1
2
3
4
5
function tb_count() {
global $wpdb;
$count = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = 'pingback'";
echo $wpdb->get_var($count);
}

Once the file is saved, you can call the function anywhere you want:

1
<?php tb_count(); ?> trackbacks since January 2006.

Displaying the Total Number of Categories

Nothing hard to get the category count. As for the posts and comments count, let’s start to create a variable which contains the result of the get_var() method.

1
2
$numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
if (0 < $numcats) $numcats = number_format($numcats);

Once you have the $numcats variable containing the number of categories, you should use it that way:

1
echo "My blog have ".$numcats." categories";

Bonus: Creating a Shortcode to Display The Total Number of Posts

Do you enjoy WordPress shortcodes? I do. Shortcodes were introduced in WordPress 2.5 and looks like “bbCodes”:

1
[url href="http://myblog.com"]My Blog[/url]

But shortcodes are also a very nice and easy way to be able to call php functions directly from WordPress html editor. This isn’t the purpose of this article, so I’m not going to explain how shortcodes works, but you should definitely have take a look at my shortcodes-related post.

Let’s create our shortcode function: Open the functions.php file from your theme and add the following code in it. Again, if your theme doesn’t have a functions.php file, create one.

1
2
3
4
5
6
7
function nbPosts() {
global $wpdb;
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);
return $numposts;
}
add_shortcode('numposts', 'nbPosts');

Then, when you’re editing a post or a page, switch to HTML mode and add the following:

1
There's [numposts] posts on this blog since I launched it.

So there you have it. The stats will automatically update themselves without you having to look near the code again. Just plug in your monthly pageview and visitor stats along with a contact form and your advertise page is complete.

Do you have an Advertise page on your blog? Let us know what stats you offer to potential advertisers.

Update: One of our readers, Wesley, has since created a plugin based on the stats here to make the advertise page for you automatically. It also has a few extras like Alexa rank thrown in!

About the author: Jean-Baptiste Jung is a 27 year old blogger from the French-speaking part of Belgium. Jean blogs about WordPress on WpRecipes and about Web Design and Development on CatsWhoCode.com. You can follow him on Twitter.

Enjoy this post? You should follow me on Twitter!