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. Hi,

    ich habe den Code von dir mal in meine Sidebar eingebaut und das klappt prima.

    Nur eine Frage.
    Gibt es eine Möglichkeit, dass ich gewisse Seiten ausblende?
    Ich würde gerne die Startseite und die Loginseite ausblenden.

    Vielen Dank und Grüße

    Detlef

  2. Danke für den tollen Codeschnipsel. Habe das mal probehalber bei mir versucht und konnte zwei Phänomene feststellen.

    Zum einen ist die Anzahl der angezeigten Kommentare immer gleich (entweder alle 3 Kommentare oder auch manchmal 5 Kommentare unabhängig von der wirklichen Anzahl der Kommentare) und zum anderen ist der erste Eintrag der Liste immer leer.

    Woran könnte das liegen?

  3. Das mit der Anzahl der Kommentare verstehe ich grad nicht so ganz – vor allem, weil es hier gar nicht um Kommentare, sondern um Top-Posts geht!? :D Alle 3? Manchmal 5? Versteh’ irgendwie nicht, was Du mir sagen willst. ;)

    Wenn die erste Zeile leer ist, liegt das daran, daß deine Top-Seite die Startseite ist. Und da die nun mal keinen Titel hat, wird auch keiner angezeigt. Das Problem hatte ich hier auf der Seite vor zwei Tagen auch festgestellt und habe den Code deshalb nochmal leicht abgeändert (eigentlich nur Zeile 8):

    
    

    Beliebte Artikel

  4. Prima, mit der Funktion “empty” werden keine leeren Beiträge mehr angezeigt. Eigentlich hatte ich die Startseite in deinem Beispiel oben bereits ausgeklammert, vielleicht habe ich die falsche ID angegeben.

    Zu den Kommentaren: In deinem Codebeispiel werden ja die Anzahl der Kommentare zum jeweiligen angezeigten Artikel mit ausgegeben. Doch die Anzahl der angezeigten Kommentare stimmt nicht und ist bei allen ausgegebenen Artikel ebenfalls gleich. Quasi als hätten alle 5 der beliebten Artikel x Kommentare.

  5. Achsoo, jetzt hab’ ich’s kapiert. ;)
    Hmm, das ist aber komisch – wie Du hier ganz unten auf der Seite siehst, funktioniert es ja bei mir richtig. Habe im Moment keine Idee, woran das bei Dir liegen kann. Hast Du ganz sicher kein Fehlerchen drin? :) Ich grübel mal weiter …

  6. Hi i would like to know how can i implement timthumb in this ? My timthumb code looks like this

    <?php if( get_post_meta($post->ID, "thumb", true) ):?>
    
    <a href=""><?php ?> alt="<?php the_title(); ?>" /></a>
    <?php elseif ((preg_match("/ggpht.com/i", getImage('1'))) || preg_match("/blogspot.com/i", getImage('1'))): ?>
    
    <a href=""><?php ?> alt="<?php the_title(); ?>" /></a>
    <?php elseif(!getImage('1') == ""): /* If the image is present elsewhere */ ?>
    
    <a href=""></a>
    <?php else: ?>
    
    <a href=""></a>
    <?php endif; ?>

Leave a Reply

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