| 
singapore-business-database

How-to show popular posts on your WordPress blog?

Yesterday I replaced my list of current PHP articles on this blog with a list of popular articles. I’m sure it’s possible in WordPress to get those list if you count the number of comments, but actually I was looking for solution based on real traffic.

Since I’m using WordPress.com Stats, I would like to use the rankings generated by this service or plugin. Searching Google, I found some widget called “WordPress.com Stats: Top Posts Widget” which works out of the box (if like to use a widget). In my case I have a custom sidebar with different custom sections using custom code. The following example explains how-to use that code on your website.

First your need to install the WordPress.com Stats plugin

Yes, the WordPress Stats plugin is required, because the list is based on that data. It’s possible that you need to wait a couple of days before you can continue with the code below (the system need to store some data first).

The tutorial code

Locate the place where you like to add the list; that can be inside the sidebar, the footer or maybe on your blog’s homepage. Add this code snippet:

<?php 
echo '
<h2>Most Popular Articles</h2>
<ul>';
$max = 5;
$top_posts = stats_get_csv('postviews', "days=7&limit=10");
foreach($top_posts as $post) {
	$post_obj = get_post($post['post_id']);
	if (isset($post_obj) && $post_obj->post_type == 'post' && $cnt < $max) {
		echo '
<li><a href="'.$post['post_permalink'].'">'.$post['post_title'].'</a> ('.$post['views'].')</li>';
		$cnt++;
	}
}
echo '
</ul>';
?>

WidgetReady

This snippet is very easy to explain, first let us look on the first two rows:

$max = 5;
$top_posts = stats_get_csv('postviews', "days=7&limit=10");

We created a variable that holds the number of entries we like to show. In Inside the function we’re able to enter values for the time period and a limit for the number of posts. This number is higher than the number we defined before. Running that function will create an array named $top_posts with this kind of key/value pairs:


[0] => Array (
[post_id] => 3933
[post_title] => Sending e-mails via SMTP with PHPmailer and Gmail
[post_permalink] => http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
[views] => 819
)
[1] => Array (
[post_id] => 5249
[post_title] => Parse html with PHP preg_match_all()
[post_permalink] => http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/
[views] => 555
)
[2] => Array (
[post_id] => 3249
...
)

Actually this array has all the information we need for our list, it’s possible that this array contains other pages too. We need to get the post type for each post and use that value as a filter inside the “foreach” loop. The “if” is also the control source to show only the maximum number of posts as defined before. Our limit (10) inside the function is higher than the “real” limit, this way we have “enough” possible elements to work with.

What are the advantages?

It depends on the traffic you get. If one post is getting most of your site’s traffic, is the chance bigger that more people will share your article with others. This activity might have the result of more traffic to the same post and of course other posts. There is also a great advantage for your on-site SEO, because these popular posts are listed on your homepage (and maybe site-wide), they get much more “link juice” than without these links. This authority is good for all pages linked from your popular pages. Similar Posts:

  • Using WordPress widgets for an improved blogging experience
  • Optimizing your WordPress Blog for Google: Part 1
  • WordPress categories, optimization tips and custom pages


Author's Website