A blogger not only wants lot of visitors but also wants to stay longer on blog. For this purpose he uses number of widgets to attract visitors to surf more and more pages and posts. Random Post Widget is the popular widget for visitors to stay longer on your pages. This widget shows different posts every time when a visitor refresh page or surf another page. In this tutorial, we will see a script which will show random posts on your blog.
How to Add this Widget?
Follow the steps below to display random posts widget in your blog's sidebar:- Login to Blogger
- Goto Layout >> Add a Gadget (in the side bar section)
- Select HTML / JAVASCRIPT in the list
- Paste the following code in the content section and save.
<script type="text/javascript">
var randarray = new Array();var l=0;var flag;
var numofpost=6;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>
- You have successfully installed the widget.
Important:
In the code var numofpost=6 is responsible for number of posts shown in widget. Change 6 with the number suited you.Get Latest Updates
Great Sir..
ReplyDelete