Add Random Posts Widget to Blogger

The Random Posts widget or gadget for Blogger will display random posts added to your blog that, due to the natural structure of blogs, could get lost easily deep in your archives. Most of the time, when new people join your site, they rarely bother going back to find one of your good posts.

One of the easiest way to give a new life to your older posts is to display them in a random order in the sidebar. So, in this tutorial, I will show you how to add a random posts gadget in a Blogger blog. The advantages of this widget is that it can be easily customized and is loading pretty fast, besides displaying the thumbnails of your posts and how many comments they have received.

Do you want to add it? Just follow these steps below:

Add Random Posts Widget to Blogger

How to add Random Posts Widget to Blogger

Step 1. Log in to your Blogger account > go to 'Layout', click on the 'Add a Gadget' link:

random posts widget, random blogger

Step 2. Add a new 'HTML/JavaScript' gadget
random posts blogger

Step 3. Paste the following code inside the empty 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 = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicKuJpcrAxvflj7hRRQMuyOjg6vmCbiMr3qdVm8dn7glQl3Bb1DVlRxxE-jiNdsNkv6ojBDua1ATXRPEGAoBKXIr8prW6BolfYjllb6MLDsM03ShvPE1eithrM9-6hi5vJv_ZhG0eUs5w/s1600/no_thumb.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://bloggerchive.blogspot.com/2012/03/add-random-posts-widget-to-blogger-blog.html">Random Posts Widget</a></div>

Customize the Random Posts Widget

If you want to show more than 5 posts, replace the 5 parameter.
To make the thumbnails bigger, change the pixels in red from width:65px; and height:65px;
To add the text snippets, change the no to yes in this line:
randomposts_details2='no';
To add more characters to the snippets, change the 60 parameter.
If you want to hide the post dates and comments, change yes to no, in this line:
var randomposts_details='yes';

Step 4. Press Save and that's it! Now you can enjoy this random posts widget on your Blogger blog.

Comments/disqusion
63 comments

  1. says:

    Thank you a lot , this is the best widget i have found of this kind ! Well done !

  2. says:

    Thank you too :)

  3. says:

    One question mate . can i make the thumnails a bit larger ?

  4. says:

    Sure. Search for the code from step 4 and increase width:36px; and height:36px value.

  5. says:

    Thank you a lot man , you made my day :)

  6. says:

    Hello man , me again , i switched to Intense Debates comment system , so i had to disable blogger default comment system , is there any way this script to work with the new comment system ? I really love the way how this script shows the number of comments so i will appreciate your help only if it's possible.

  7. says:

    Well , changed to Disqus comment system meanwhile , so if you can help me i will be so glad.

  8. says:

    Sorry... i've tried to change some codes in the widget to be compatible with disqus but i don't know any solution at this moment. If i'll find one, i'll write you back.

  9. says:

    ok man , your widgets are great , i use related posts one too

  10. says:

    Thanks alot, it worked wonderfully.

  11. says:

    You're welcome!

  12. says:

    how can i add the first 200 letters from any post?

  13. says:

    how can i add the first 200 letters from any post?... and how can i hide the author name, and the number of comments ?

    Thanks in advange! :)

  14. says:

    I need the floating type facebook popup you implemeted on the blog right side.

    Could you please consider sharing the code??

    to livepositiveway@gmail.com

  15. says:

    Great widget! I tried it on my own blog too....it's working great.

  16. says:

    dear admin, this for this widget, and it has fully functional on my blog, but i need to do some changes on this widget,it seems like the image is scaled to square s72-c,what should i do to change it so the image wouldn't be scaled to square, i shall show the image as s300,not thumbnail,please give me some advice,thx

  17. says:

    Love this widget, but any way to make the pictures less pixely? I have really HQ pics up, but they look stretched and amateurish. Plz help!!

    xx
    Sarah

  18. says:

    how to diseable the comment and author ?

  19. says:

    Search for this line var rdp_info='yes'; and change 'yes' to 'no'. Thanks for visiting!

  20. says:

    Thanks alot, it worked wonderfully.

  21. says:

    how can i fade out that comment-stat in the widget? i dont want it to appear there. thanks in advance.

  22. says:

    i need a gadget to show 10 posts before a post is made and 10 after the post was made ...any idea...

  23. says:

    Thanks so much!

  24. says:

    Thanks for this widget, you help me to get more less bounce out.

  25. says:

    My pleasure, thanks for visiting :)

  26. says:

    Nice Widget !

    But the problem is that it has no link when you get your mouse cursor on thumb. Can you tell me how can I give links on thumb also ?

    Thanks

  27. says:

    thanks a lot....been looking for this

  28. says:

    Finally a random post widget for blogger that really works and can be customized!
    Thank you!

  29. says:

    Thank you for this tutorial. Unfortunately, using this script on any of my blogs causes the page (and even the blogger dashboard) to completely crash.

    Any idea why this might be happening? I'm not a master of web coding but my guess is that there is a script conflict perhaps?

  30. says:

    great thanks alot

  31. says:

    Thanks for this script. I have been looking for this widget..

  32. says:

    Thanks.. I want to disable comment too :)

  33. says:

    Can you tell me how to change title front size please? Thanks before..

  34. says:

    Good widget and clear directions for installing it!

    How do I remove the pictures? Most of my posts don't have images.

    Thanks

  35. says:

    how using it into HTML ?

  36. says:

    Just needed the code for random posts and here it is... Thanks for sharing it....

  37. says:

    Hello,
    As we use DISQUS system can we modify the code to ignore comments and author and just provide the image, title and snippet?
    Many thanks in advance.
    BTW I set var rdp_info to no but didn't help.

  38. says:

    hi i used this script but this is slowing my site down i put this code all the way on bottom still for some reason it slows the lagg time

  39. says:

    Thank you. :)

  40. says:

    Remove the line:

    document.write('< img alt="'+rdp_posttitle+'" src="'+rdp_thumb+'"/>');

    I had to add a space after the first < so it would accept my comment.

  41. says:

    great widget. thanks for sharing

  42. says:

    thank you

  43. says:

    Brilliant. Thanks a lot!!Just wondering if I make only some specific posts in a specific category show on random posts widget? Anyway thanks for sharing nice info again!

  44. says:

    bu güzel widget için teşekkürler.

  45. says:

    i follow all the steps, but when i view my blog the pictures of my post keep showing on top of the title post, its not like the picture above?

  46. says:

    Finally a random post widget for blogger that really works and can be customized!
    Thank you!

  47. says:

    very good job!, Thanks !

  48. says:

    nice widget.. working great in my blog.

  49. says:

    but I want only simple random post without thumbnail can you plz help me. I want only the post name no thumbnail, no date and no info of the post only of post title.

  50. says:

    Your blog is wonderful. I like it.

  51. says:

    Hi, how to disable these descriptive balloons were subject to the same links and graphics?

  52. says:

    Is there a way to filter the time frame of random post let say, only 3 months posts

  53. says:

    This is awesome. Thanks so much for sussing it out!

    How would I go about changing the date format to standard American style (i.e. month/day/year rather than day/month/year)?

  54. says:

    Hi there,

    Replace this part in the script (press CTRL + F to find it):

    '<span>'+rdp_postdate.substring(8,10)+'/'+rdp_postdate.substring(5,7)+'/'+rdp_postdate.substring(0,4)+' - '+rdp_commentsNum)+'</span>'

    With this one:

    '<span>'+rdp_postdate.substring(5,7)+'/'+rdp_postdate.substring(8,10)+'/'+rdp_postdate.substring(0,4)+' - '+rdp_commentsNum)+'</span>'

    Thanks for visiting!

  55. says:

    Open quote is expected for attribute "type" associated with an element type "script"

    how to solve this error.. please help me

  56. says:

    how to use this widget for particular label.

  57. says:

    This is an excellent widget thanks so much for sharing this. For those that would like to change the image not found to a custom image. Resize an image you want to use to 170x170px then upload that image to any one of your blog posts in the blogger html editor. You don't need to save the post just copy the image link code after it has uploaded. Search for this in the code in the script http://3.bp.blogspot.com/-5SoVe1K6JSk/Utl0OOmucAI/AAAAAAAAF6E/hQghgD_EJdQ/s1600/no_thumb.png and replace it with the link to the image you uploaded. For an example check out http://mkvXstream.com where you will see our logo instead of the image not found. It's a good way to promote your blog brand and help readers remember your site name. Has anyone found out how to disable the snippet_length=150; completely? I changed it to 0 although it still shows ...

  58. says:

    Many thanks! It's all functioning like a champ now!

  59. says:

    Assalumu-Alkum, i have a problem with this widget, after add this widget the thumbnails of videos don't show. i am sure that this can be due to dailymotion videos. because with youtube videos this can work correct. if you can help me for this, i will great thankful to you. Good Bye

  60. says:

    Is there a way to align the thumbs with the left of the widget? Right now they're about 10-20px over to the right. I'd like to have them align all the way to the left,

  61. says:

    Hi Annelise,

    You will need to change the 'margin-right' pixels from this line to 0:
    #random-posts img{float:left;margin-right:10px;
    width:65px;height:50px;background-color: #F5F5F5;padding: 3px;}

  62. says:

    it works! thanks!

    have a great day.

  63. says:

    How do i show a random posts from a different blog