Home How To Blogging Tips SEO

Pages

Aug 15, 2012

Code for Random Posts

This Javascript code takes your blog or website feeds as input and displays random post titles. Adding this code to your blog or website is pretty easy. Whenever user visits your website he/she will see some random post titles of your blog. Thus if user finds any interesting post title he/she clicks on it and reads that post too thus increases user engagement with your blog or website.

Blogger Blog go to your blogger dashboard, click on layout, add a HTML/Javascript gadget, paste the following code and lastly click on save.

Wordpress blog add html/Javascript adder plugin and then go to your dashboard click on Appearance click on widgets there you will find html/Javascript adder drag it to side bar, paste the following code and click on save . Others you can copy the code wherever you want on your website in a HTML editor.

Code for Random Posts


<script type="text/javascript">
var randarray = new Array();var l=0;var flag;
var numofpost=8;function randomposts(json){
var total = parseInt(json.feed.openSearch$totalResults.$t,10);
for(i=0;i < numofpost;){flag=0;randarray.length=numofpost;l=Math.floor(Math.random()*total);for(j in randarray){if(l==randarray[j]){ flag=1;}}
if(flag==0&&l!=0){randarray[i++]=l;}}document.write('<ul>');
for(n in randarray){ var p=randarray[n];var entry=json.feed.entry[p-1];
for(k=0; k < entry.link.length; k++){if(entry.link[k].rel=='alternate'){var item = "<li>" + "<a href=" + entry.link[k].href + ">" + entry.title.$t + "</a> </li>";
document.write(item);}}
}document.write('</ul>');}
</script>
<script src="/feeds/posts/default?alt=json-in-script&start-index=1&max-results=1000&callback=randomposts" type="text/javascript"></script>

In the above code 8 represents no of post titles you can replace it with your desired number.
For example you can see some random post titles in my blog as shown in below pic when you refresh the page you can see the change in post titles
Code for Random Posts
You May Also Like
1. Get Previous and Next Post Titles in Blogger

10 comments:

  1. Thanks Anil. It worked for blogger, but for WordPress.com , it does not.

    ReplyDelete
    Replies
    1. @Pattu Raj - For wordpress first you need to install html/javascript adder plugin then it will be shown in widgets. Then you have to copy this code in that widget.

      Delete
  2. Hey Anil. Thanks for the code. I tried it on my blogger but it shows a random number of posts: sometimes ten, sometimes only 2 or none at all.

    ReplyDelete
    Replies
    1. Hi Ladaisi, have you changed numofpost count as marked green in above code?

      Delete
    2. Yes, I changed it to 10, but it randomly shows anywhere between 0 and 10 posts upon refreshing ... I'm so confused!

      Delete
    3. Total how many posts are there in your blog?

      Delete
    4. I designed it, so ... customized?

      Delete
  3. This code worked for blogger classic template.. but i'm not sure about other templates. sry for that :(

    ReplyDelete

Criticism Accepted :)