Deutsch English
WordPress Diesen Artikel auf Deutsch lesen

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:

1
2
3
4
5
6
7
8
9
10
11
12
<?php if(function_exists('stats_get_csv')) : ?>
	<h3>Beliebte Artikel</h3>
	<ul id="popularposts">
	<?php
		$top_posts = stats_get_csv('postviews', 'days=-1&limit=5');
		foreach($top_posts as $post) {
			if(!get_post($post['post_id']) || empty($post['post_id'])) continue;
			echo '<li><a href="'.get_permalink($post['post_id']).'">'.get_the_title($post['post_id']).'</a><div class="commentsnumber">('.get_comments_number($post['post_id']).' Kommentare)</div></li>';
		}
	?>
	</ul>
<?php endif; ?>

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:

<?php if(function_exists('stats_get_csv')) : ?>
	<h3>Beliebte Artikel</h3>
	<ul id="popularposts">
	<?php
		$top_posts = stats_get_csv('postviews', 'days=-1&limit=10');
		$i = 0;
		foreach($top_posts as $post) {
			if(!get_post($post['post_id']) || empty($post['post_id']) || in_array($post['post_id'], array(1, 17, 25, 39))) continue;
			echo '<li><a href="'.get_permalink($post['post_id']).'">'.get_the_title($post['post_id']).'</a><div class="commentsnumber">('.get_comments_number($post['post_id']).' Kommentare)</div></li>';
			$i++;
			if($i >= 5) break;
		}
	?>
	</ul>
<?php endif; ?>

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.



Comments

1 2

  1. 5th September 2011
    8:25 pm

    Ginchen flag

    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. ;)

  2. 5th September 2011
    6:05 pm

    vanvox flag

    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

  3. 5th October 2010
    4:51 pm

    Ginchen flag

    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 = '<a href="'.$STUFF_MISSING_HERE.'" alt="'.get_the_title($post['post_id']).'" />IMAGE MISSING HERE!?</a>';
    } elseif ((preg_match("/ggpht.com/i", getImage('1'))) || preg_match("/blogspot.com/i", getImage('1'))) {
        $imagelink = '<a href="'.$STUFF_MISSING_HERE.'" alt="'.get_the_title($post['post_id']).'" />IMAGE MISSING HERE!?</a>';
    } elseif(!getImage('1') == "") {
        $imagelink = '<a href="">WHATEVER MISSING HERE!?</a>';
    } else {
        $imagelink = '<a href="">WHATEVER MISSING HERE!?</a>';
    }

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

    echo '<li><a href="'.get_permalink($post['post_id']).'">'.$imagelink.'</a><div class="commentsnumber">('.get_comments_number($post['post_id']).' comments)</div></li>';

    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`.

1 2

Write comment

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong> <img src="" alt="" class="" width="" height=""> | Code snippets can be posted in `backticks`. Example: `<?php echo "Hi!"; ?>`