Integrate WordPress statistics into a template plugin- and widget-free


For a while I’ve been looking for a way to integrate the top-posts of my site, that have been established by the WordPress stats plugin, into my template. I wanted to have a list of the five most visited posts, preferably with the number of comments.
I remembered that I had once seen a plugin for that: WordPress Popular Posts. But when I installed it, I found that in the latest version, it works exclusively as a widget and does not provide a template tag. That way, of course, it was of absolutely no use to me, because my template is not and will never be intended for widgets. And simply installing an older version of the plugin is not a permanent solution either.

But as usual, I thought to myself: “Man, there must be some way to do this!” And there is one indeed! With just a few lines of code, you can integrate your top posts from the WordPress stats plugin at any place in your template, without any additional plugin!

Here’s how:


	

Popular Posts

Short description:

  • Line 1: The entire list will be created only if the WordPress stats plugin is installed and activated.
  • Line 5: Here you define which or how many posts are shown. If you set “days” to -1, you get the top posts of all time, ever since the stats plugin has started collecting data. However, if you set it to 7, for example, you get the top posts of the last seven days. The value “limit” indicates how many posts you would like to display. So in the example, the five most popular posts are shown.
  • Line 8: Here we output the link with the number of comments. Of course you can adjust the HTML depending on your needs.

Update (17 March 2010)

Someone asked if it is also possible to exclude certain pages from the top list. The stats plugin itself does not provide an “exclude” option. But of course there is a way:


	

Popular posts

First you have to increase the limit at least by the number of excluded pages. If in doubt, rather set it a little too high than too low. Then enter the IDs of the pages you wish to exclude into the array, where I wrote “1, 17, 25, 39” as an example. If necessary, replace the 5 in the line if($i >= 5) break; by the desired number of top sites, in case you wish to display more or less than five top posts.


16 responses to “Integrate WordPress statistics into a template plugin- and widget-free”

  1. I have absolutely no idea what timthumb is. And you didn’t post your code in `backticks`, so it’s quite scrambled now. I tried to repair it a little, but … well.

    Anyhow, I would try something like this:

    if(get_post_meta($post['post_id'], "thumb", true)) {
        $imagelink = 'IMAGE MISSING HERE!?';
    } elseif ((preg_match("/ggpht.com/i", getImage('1'))) || preg_match("/blogspot.com/i", getImage('1'))) {
        $imagelink = 'IMAGE MISSING HERE!?';
    } elseif(!getImage('1') == "") {
        $imagelink = 'WHATEVER MISSING HERE!?';
    } else {
        $imagelink = 'WHATEVER MISSING HERE!?';
    }
    

    Then put that code before line 8, and edit line 8 to something like this:

    echo '
  2. '.$imagelink.'
    ('.get_comments_number($post['post_id']).' comments)
  3. ';

    Anyway, don’t just copy and paste this, because, as you can see, there’s still stuff missing in there (the upper-case stuff) that I wasn’t able to find within the code you posted. You will have to fill that in yourself or post the whole code here again in `backticks`.

  4. Danke für den Tipp. Ich habe den code jetzt bei mir in der Sidebar eingebaut. Es funktioniert, solange ich bei days keine -1 angebe. Da werden nur 2 Beiträge geliefert. Wo finde ich eine Spezifikation von der Funktion? Habe schon das halbe WWW durchsucht…
    Grüße Christina

  5. Hi,
    eine Dokumentation gibt es zu der Funktion wohl leider nicht, soweit ich weiß. Ich habe einfach nur einen Blick in den Quellcode des Stats-Plugins geworfen und mir das Ganze dann selbst zusammengebastelt. Habe gerade auch noch mal nachgeguckt, ob da evtl. inzwischen was geändert wurde, so dass die -1 nicht mehr funktioniert – aber nichts. Eigentlich sollte es noch gehen. Ich werd’ das die Tage noch mal genauer untersuchen und dann Bescheid geben. ;)

Leave a Reply

Your email address will not be published. Required fields are marked *