How To Add Random Posts Widget In Blogger

A random post widget helps your previous articles to be alive. When you keep writing post on your blog then after sometime your previous articles got buried deep under the directory and becomes difficult for the readers to reach them. Adding a random post widgets will help those articles to get a regular traffic. Random post widgets shows some of your post links randomly. It means everytime a user visit any page, it will show some links to your other posts and these links keep changing randomly everytime.

How To Add Random Posts Widget In Blogger

Here is a tutorial which will help you to add randome post widget on your blog.

Add Random Post Widget in Blogger

Step 1 : Open your blogger dashboard. Go to “Layout” and then click on “Add a widget”.
 Add Random Posts Widget In Blogger
Step 2 : You will be shown a list of widgets. Select “HTML/Javascript” from the list as shown in picture below.
How To Add Random Posts Widget In your blog
Step 3 : Copy and paste the following code in that box.
              <style>

#random-posts img {
    float: left;
    margin-right: 10px;
    width: 65px;
    height: 65px;
    background-color: #F5F5F5;
    padding: 3px;
}

ul#random-posts {
    list-style-type: none;
}

#random-posts li {
    margin-bottom: 10px;
}

.random-summary {
    display: block;
}
</style>

<ul id=’random-posts’>
<script type=’text/javaScript’>
var randomposts_number = 5;
var randomposts_chars = 60;
var randomposts_details = ‘yes‘;
randomposts_details2 = ‘no‘;
var randomposts_comments = ‘Comments’;
var randomposts_commentsd = ‘Comments Disabled’;
var randomposts_current = [];
var total_randomposts = 0;
var randomposts_current = new Array(randomposts_number);

function randomposts(json) {
    total_randomposts = json.feed.openSearch$totalResults.$t
}
document.write(‘<script type=”text/javascript” src=”/feeds/posts/default?alt=json-in-script&max-results=0&callback=randomposts”></script>’);

function getvalue() {
    for (var i = 0; i < randomposts_number; i++) {
        var found = false;
        var rndValue = get_random();
        for (var j = 0; j < randomposts_current.length; j++) {
            if (randomposts_current[j] == rndValue) {
                found = true;
                break
            }
        };
        if (found) {
            i–
        } else {
            randomposts_current[i] = rndValue
        }
    }
};

function get_random() {
    var ranNum = 1 + Math.round(Math.random() * (total_randomposts – 1));
    return ranNum
};
</script>
<script type=’text/javaScript’>
function random_posts(json) {
    for (var i = 0; i < randomposts_number; i++) {
        var entry = json.feed.entry[i];
        var randompoststitle = entry.title.$t;
        if (‘content’ in entry) {
            var randompostsnippet = entry.content.$t
        } else {
            if (‘summary’ in entry) {
                var randompostsnippet = entry.summary.$t
            } else {
                var randompostsnippet = “”;
            }
        };
        randompostsnippet = randompostsnippet.replace(/<[^>]*>/g, “”);
        if (randompostsnippet.length < randomposts_chars) {
            var randomposts_snippet = randompostsnippet
        } else {
            randompostsnippet = randompostsnippet.substring(0, randomposts_chars);
            var whitespace = randompostsnippet.lastIndexOf(” “);
            randomposts_snippet = randompostsnippet.substring(0, whitespace) + “&#133;”;
        };
        for (var j = 0; j < entry.link.length; j++) {
            if (‘thr$total’ in entry) {
                var randomposts_commentsnum = entry.thr$total.$t + ‘ ‘ + randomposts_comments
            } else {
                randomposts_commentsnum = randomposts_commentsd
            }; if (entry.link[j].rel == ‘alternate’) {
                var randompostsurl = entry.link[j].href;
                var randomposts_date = entry.published.$t;
                if (‘media$thumbnail’ in entry) {
                    var randompoststhumb = entry.media$thumbnail.url
                } else {
                    randompoststhumb = “http://1.bp.blogspot.com/-M1vn74_5DEA/VN2d3-tlvRI/AAAAAAAACDA/9WfNvVPIOv0/s1600/no%2Bimage.png”
                }
            }
        };
        document.write(‘<li>’);
        document.write(‘<img alt=”‘ + randompoststitle + ‘” src=”‘ + randompoststhumb + ‘”/>’);
        document.write(‘<div><a href=”‘ + randompostsurl + ‘” rel=”nofollow”>’ + randompoststitle + ‘</a></div>’);
        if (randomposts_details == ‘yes’) {
            document.write(‘<span>’ + randomposts_date.substring(8, 10) + ‘.’ + randomposts_date.substring(5, 7) + ‘.’ + randomposts_date.substring(0, 4) + ‘ – ‘ + randomposts_commentsnum) + ‘</span>’
        }
        if (randomposts_details2 == ‘yes’) {
            document.write(‘<span class=”random-summary”>’ + randomposts_snippet) + ‘</span>’
        }
        document.write(‘<div style=”clear:both”></div></li>’)
    }
};
getvalue();
for (var i = 0; i < randomposts_number; i++) {
    document.write(‘<script type=”text/javascript” src=”/feeds/posts/default?alt=json-in-script&start-index=’ + randomposts_current[i] + ‘&max-results=1&callback=random_posts”></script>’)
};
</script>
</ul>
<div style=”font-size: 10px; float: right;”><a href=”http://helplogger.blogspot.com/2012/03/add-random-posts-widget-to-blogger.html” rel=”nofollow”>Random Posts Widget</a></div>

Step 4 : Save the widget. That’s it. You are done with it. You can check your blog now and see that random post widget.

Customization

1. If u want to show more or less than 5 post then change the number 5.
2. To change the size of thumbnails, change the value of pixels in width:65px and height:65px
3. To hide the date and comments, change yes to no in this line ;
   var randomposts_details=’yes‘;
4. To add the text snippets, change no to yes in this line;
    randomposts_details2=’no‘;
    to show more or less characters change the value 60

Add a Comment

Your email address will not be published. Required fields are marked *