Seit einer Weile habe ich nach einer Möglichkeit gesucht, die Top-Posts meiner Seite, die das WordPress-Stats-Plugin ermittelt hat, in mein Template einzubinden. Ich wollte gerne eine Liste der fünf meistbesuchten Posts haben, am besten noch mit der Anzahl der Kommentare.
Ich erinnerte mich, daß ich dafür schon einmal ein Plugin gesehen hatte: WordPress Popular Posts. Aber als ich es installiert hatte, mußte ich feststellen, daß es in der neusten Version ausschließlich als Widget funktioniert und kein Template-Tag zur Verfügung stellt. Das brachte mir natürlich überhaupt nichts, denn mein Template ist nicht für Widgets vorgesehen, und das soll sich auch nicht ändern. Und eine ältere Version des Plugins zu installieren, ist ja nun auch keine dauerhafte Lösung.
Doch wie immer dachte ich mir: „Mensch, das muß doch auch so gehen!“ Und das tut es auch! Mit ein paar wenigen Codezeilen kann man seine Top-Posts vom WordPress-Stats-Plugin an jeder beliebigen Stelle seines Templates einbinden, ganz ohne irgendein zusätzliches Plugin!
[:] [:en]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!
[:] [:de]Und so geht’s:
Beliebte Artikel
-
'.get_the_title($post['post_id']).'
Kurze Erläuterung
- Zeile 1: Die ganze Liste wird nur erstellt, wenn das WordPress-Stats-Plugin installiert und aktiviert ist.
- Zeile 5: Hier wird definiert, welche bzw. wie viele Posts angezeigt werden sollen. Stellt man „days“ auf -1, bekommt man die Top-Posts der gesamten Zeit, in der das Stats-Plugin Daten gesammelt hat. Stellt man es aber z. B. auf 7, bekommt man die Top-Posts der letzten sieben Tage. Der Wert „limit“ gibt an, wie viele Posts man anzeigen möchte. In dem Beispiel werden also die fünf beliebtesten Posts angezeigt.
- Zeile 8: Hier erfolgt die Ausgabe des Links mit Anzahl der Kommentare. Natürlich kann man das HTML je nach Bedarf noch anpassen.
Update (17. März 2010)
Es wurde gefragt, ob man auch bestimmte Seiten ausschließen kann. Das Stats-Plugin selbst sieht eigentlich kein „exclude“ vor. Aber es gibt natürlich auch hierfür einen Weg:
Beliebte Artikel
-
'.get_the_title($post['post_id']).'
Zunächst muß man also das Limit mindestens um die Zahl der ausgeschlossenen Seiten erhöhen. Im Zweifelsfall lieber ein wenig zu hoch als zu niedrig. Danach trägt man in das Array, in das ich jetzt beispielhalber „1, 17, 25, 39“ geschrieben habe, die IDs der auszuschließenden Seiten ein. Gegebenenfalls ersetzt man noch in der Zeile if($i >= 5) break;
die 5 durch die gewünschte Anzahl an Top-Seiten, falls man mehr oder weniger als fünf Top-Posts anzeigen möchte.
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 Antworten zu “[:de]WordPress-Statistiken plugin- und widgetfrei ins Template einbinden[:en]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. […]