wayne on September 1, 2008
I decided to put together a BlogEngine extension that will display the top posts based on the number of comments they've received. I've been wanting this extension for a long while, and I decided to bite the bullet and put it together myself.
Why isn't there a Top Post class for BlogEngine?
I've looked around quite a bit for this component and to my surprise, I never found one. The closest I've come to something similar is Cristiano Fino's Top Commenters component for BlogEngine. After the amazement of not finding an existing component wore off, I decided to put one together.
Mind you, I'm still getting comfortable working within BlogEngine, and each time I do I learn a little more about the engine itself. If this extension could benefit from any code changes, please let me know.
My main point of concern is the way I refer to the Posts collection. You'll see in the following code snippet that I define a new List collection of Post objects. I then needed to sort the collection to obtain the top commented posts that define the top posts to obtain. So, to not effect the ordering for anything that may use the collection later, I make a copy of the collection and sort that instead.
// Make a copy of the posts collection so that it doesn't impact normal rendering
// after we sort it
List<Post> posts = new List<Post>();
posts.AddRange(Post.Posts);
// Sort the items based on number of comments
posts.Sort(delegate(Post post1, Post post2)
{
return Comparer<int>.Default.Compare(post2.Comments.Count, post1.Comments.Count);
});
If this method can be improved or done in a way that is more efficient, please let me know.
Why the Top Posts Extension?
I wanted this extension for a few reasons. The foremost being that I wanted to highlight some of the more popular posts found on my blog (if you can define comment count as popular). I wanted new readers to be able to see some of the more active posts within the archives. I decided display the top 5 commented posts.
Another reason was to punch through an extension for BlogEngine. I've been sitting on doing one for a long while now, and I finally found the time and drive to bust this out.
Finally, I also wonder if this will be included in the core. I find it remarkable that it isn't there already. Perhaps this will inspire the devs to include it?
Implementing the Top Posts Extension
Implementing the extension is easy. Simply copy the TopPost.cs file to your App_Code/Extensions folder. Once there, you can then place the following in your site.master theme file:
<blog:TopPosts MaxNumber="5" ID="TopPosts1" runat="server" />
MaxNumber - The number of posts to display
Download the Top Posts Extension
I know this will work for BE 1.3 or greater...
Single file in a zip - Download TopPosts.zip (2kb)
I've also put a Download page in place to capture any future extensions or themes I might put together.
Feature Requests
Modifying the control may be easy for some, and not so for others. If there are any changes you think would be good to include, leave a comment and let me know. I'd be happy to work them in for you.