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
-
'.get_the_title($post['post_id']).'
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
-
'.get_the_title($post['post_id']).'
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”
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:
Then put that code before line 8, and edit line 8 to something like this:
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`.
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
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. ;)
[…] möchte ich aber doch kurz schildern, wie ich den Code von Ginchen hier in diesem Blog umgesetzt habe und damit unterstreichen, wie begeistert ich von dessen Ausgabe […]
[…] findet man in der Regel in der Sidebar. Eine schöne Plugin-freie Lösung habe ich hierfür auf Ginchens Blog gefunden. Den Code, den sie da anbietet baut ihr einfach in euer Theme an der ensprechenden Stelle […]
[…] und damit möglichst Ressourcen-schonend, zeigt dieser Artikel von Ginchen. […]