Most popular this week: Star ratings rich snippet on Google SERP with Graddit (for Blogger).

Sunday, November 4, 2012

How to sort blogger posts by popularity

Blogger offers 2 options to sort your posts: sort by newest and sort by oldest. With Graddit widgets you can sort posts by their popularity. Look at this page: It displays 5 top articles from this blog. And this one: shows best posts announcements tagged with "blogspot".
These aren't regular posts, these are Bloggers static pages. If you'll open their source code you'll see that there are no actual posts, only some javascript code. To create your own pages like those you'll need:
  1. create an account if you still don't have it;
  2. create a desired top here;
  3. add graddit-extras.js into <head> section of your template:
    <script src='' type='text/javascript'/>
  4. create static HTML page, switch to HTML edit mode and add jQuery, if you don't have it already in your template:
    <script src='' type='text/javascript'/>
  5. add JS function that will process your posts:
    <script type='text/javascript'>
    function gradditLoadTopPosts(id) {
    var element = $("#" + id);
    if (element.length > 0) {
    var wrapperDiv = $("#top5");
    if (wrapperDiv.length == 1) {
    element.find("a").each(function(i, a) {
    var host = a.hostname;
    var blogHost = "fruitfulbookmarks-en.blogspot";
    if (host.indexOf(blogHost) >= 0) {
    var docHost = + "";
    var level1Doc = docHost.substring(blogHost.length, docHost.length);
    var level1Post = host.substring(blogHost.length, host.length);
    var link = a.href;
    if (level1Doc != level1Post) {
    link = link.replace(blogHost + level1Post, blogHost + level1Doc);
    var containerId = "top_posts_" + i;
    wrapperDiv.append("<div id='" + containerId + "' style='display: none;'></div>");
    var container = $("#" + containerId);
    setTimeout(function() {
    var post = container.find("div[class='post hentry']");
    if (post.length > 0) {
    var newText = "";
    if (i > 0) {
    newText = "<hr style='width: 100%; clear: both;'/>"
    newText += gradditCutText(post.html(), 900, 2) + "<" + "br /><" + "br/>";
    var startIdx = newText.indexOf("<h3 class=\"post-title entry-title\">");
    var endIdx = newText.indexOf("</h3>");
    newText = newText.substr(0, startIdx) + "<a href='" + link + "'>" + newText.substr(startIdx, endIdx - startIdx + 5) + "</a>" + newText.substr(endIdx, newText.length);
    }, 2000 * (i + 1));
    Couple of words about changes you might need to make to this function in order to make it working with your blog:
    • #top5 - this is id if the container element, you'll see it below; depending on the template you might need to change the names of the classes;
    • in line var blogHost = "fruitfulbookmarks-en.blogspot"; replace blog URL with your own; do not include country domain (.com,, etc.)!
    • when calling function gradditCutText to cut text of your posts there are few parameters you might want to change: second parameter is max length of text, third one allows you to control how to cut it (0 - do nothing, 1 - cut to word, 2 - expand to word). If you don't want to cut text then pass both parameters equal to 0;
  6. add HTML elements and tops script call with callback function:
    <div id="top5"></div>
    <div id="graddit_top_wrapper" style="visibility: hidden; display: none;"></div><script type="text/javascript" src=""></script>
    Make sure to change top id (33 here) to your own that should be taken from administer page (when you create/manage tops).

It should be ready now. Besides that tops got new parameter: labels. Now you can specify which posts (with what tags) should be displayed in tops widgets. So (if you're familiar with JS) on a page you can display top posts filtered by categories. Try it, let me know what you think.

Rate this posting:

You might like: