Create an Auto-Stats Page for Advertisers

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.

Share

  1. Great tips Jean.. I didn’t know about most of them before ;) After this article we all have to go to update our advertise pages on blogs :) )

  2. That’s really cool tips but how about technorati rank, alexa, rss readers or maybe social bookmarks.?! ;)

  3. Thanks for this great Wordpress tips Jean. These are great add-ons for every blog to display even more real time stats. Stats page will need some traffic stats also, but for that we will need some plugins to do that part of job.

  4. Dan (4 comments)30 December 08

    Thanks for sharing ! I like to do things myself to have more control so this is definitely a great plugin-free tip. Definitely will blog about this in the next few days.
    Stumbled.

  5. Arjen (13 comments)30 December 08

    Great! Thank you!

    I’m currently working on a new custom page template for my archives, so I’ll definitely put this into it.

  6. Maigret (1 comments)2 January 09

    The more I read your articles and there is insufficient time to implement all these ideas ! Thank you so much. :cool:

  7. Marvia (1 comments)2 January 09

    @Maigret…Oh so true. I read and go Aaarrggggggghhhh..when am I supposed to do that!!

    Thanks Cat:-) I’m learning slowly but surely. You’ve been a great help.

  8. Rainbow (1 comments)3 January 09

    Very nice !

    When a book containing all these tricks? I think that it will a awesome sucess !

  9. John (6 comments)3 January 09

    That is cool stuff. thanks for sharing

  10. Great post! You really ought to add traffic stats, page rank and Alexa score and put it all in one package. You could certainly sell a pre-coded page.

    Putting together a page for advertisers is a pain in the a$$. Mine is here:

    http://pajamaprofessional.com/advertise/

    I haven’t touched mine in months and it’s terribly out-of-date and doesn’t include post or comment counts at all.

    Thanks for the great information. I will be including it in my weekly links post.

    Sara Ch.

  11. Keep up the great work ;)

  12. Ralph (9 comments)4 January 09

    Your tips are helpful for my own blog about my diploma project. Thank you. Ralph

  13. cooiky (1 comments)4 January 09

    cool job,thanks

  14. Excellent tips, thanks. And very well put – it is easy to follow, which is great to see.

  15. The stats would come in handy. But would it reduce loading time for the website?

    • Nope, not by anything noticeable. Think of how much work WordPress has to do just to pull comments on a post from the database. It won’t have any trouble executing a few queries like this.

      The thing that might slow down loading times a bit would be if you started including Alexa and Technorati widgets with JavaScript, but a lot of people seem to want them here so maybe that would be worth it? :)

  16. Wesley (4 comments)5 January 09

    Thanks so much for the article. I enjoyed it so much that I made a plugin out of this :)

    I ofcourse asked permission to the article writer first, which he agreed to.

    You can download it here: http://www.improvingtheweb.com.....log-stats/

    It includes everything discussed in the article, but also pagerank, alexa, technorati, feedburner, delicious, user count, link count, tag count, etc..

    Acknowledgements to problogdesign and wprecipes are in the plugin as well. Thanks again for the idea :)

    • That’s brilliant Wesley! I’ve updated the post now with a link to your plugin, thanks for creating it and sharing the link here. :D

      • Wesley (4 comments)6 January 09

        Thanks! If anyone can think of any other stats that should be in there, do let me know :)

  17. Definitely agree that this plugin is great :)

  18. Took notes on phone at the conference. ,

  19. Research Chevy 350 motors, engines, and purchase quality Chevy and GM 350 motors. You will find helpful information, stats, and articles about the GM Chevrolet 350 (5.7L) motor and its use on boats, trucks and passenger cars.

    chevy 350 engine

  20. Structured Settlement Quotes provides structured settlement recipients the option to sell the rights to their future annuity payments through their diversified network of private investors.”

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?