Top 10 Posts/Views
Thanks to a tip from podz, I installed Top 10 Posts/Views plugin. This gave me the functionality of showing view count for each post, and showing the 10 most viewed posts. I made a few changes to make it work right on my site.
First I followed the documentation, which was just fine, no problems there. Installing was easy and quick. Only changes I made were:
- In show_pop_posts: before the showeach block add <ul> and after it add </ul>
- In show_post_count: changed echo $before.$postcount.$after; to echo $before.(($postcount)-1).$after; (I was getting off-by-one)
- In show_pop_posts: changed $post_title = substr($post->post_title, 0, 20); to $post_title = $post->post_title; (I thought the first 20 chars of a post was rather ugly)
I also noticed an error when displaying with the default post numbers instead of permalinks, but I don’t use it, so I’m worried.
Other than that, it works exactly how I wanted. The number of queries can probably be reduced from 2*num posts shown + 1 for top10. I won’t worry about that because it doesn’t really slow me down.
Edit 6/24:
Added functionality so that if the user has already seen a post, it will only be logged (and incremented) the first time. I did this with the $_SESSION superglobal, using the following code. In add_count:
$tmp = explode("&", $_SESSION["p"]);
if(!in_array($p_number, $tmp)){
.
.
.
$_SESSION["p"] .= “$p_number&”;
}
IMO this is better than only logging if is_single() is set… i.e. if the user is only viewing that post, but it can be used in conjunction with that method for best results.
