Display random posts on your Blogger blog

  • Français
  • English

  • A very simple Javascript code that allows you to display random articles on your blog under Blogger.

    The code for random posts

    <div id="wk_random_post_widget">
    <h3>Random posts</h3>
    <ul id="wk_random_post_list"></ul>
    <script type="text/javascript">
    var wk_feed_url = "https://votreblog.blogspot.com/feeds/posts/default";
    var wk_max_posts = 5;
    var wk_random_posts = [];

    function wk_get_random_posts(json) {
    var total = json.feed.openSearch$totalResults.$t;
    var indices = [];
    while (indices.length < wk_max_posts) {
    var index = Math.floor(Math.random() * total) + 1;
    if (!indices.includes(index)) {
    for (var i = 0; i < wk_max_posts; i++) {
    var script = document.createElement("script");
    script.src = wk_feed_url + "?start-index=" + indices[i] + "&max-results=1&alt=json-in-script&callback=wk_show_random_post";

    function wk_show_random_post(json) {
    var entry = json.feed.entry[0];
    var title = entry.title.$t;
    var link = entry.link.find(function(l) { return l.rel == "alternate"; }).href;
    var li = document.createElement("li");
    var a = document.createElement("a");
    a.href = link;
    a.innerText = title;

    var script = document.createElement("script");
    script.src = wk_feed_url + "?max-results=0&alt=json-in-script&callback=wk_get_random_posts";

    Replace https://yourblog.blogspot.com/feeds/posts/default with the address of your blog under Blogger. And paste the code into the widget area of your blog. This code uses the RSS feed to scan which articles it will display at random, so it will not display all the articles if your blog is very old, but it will cover a large part of it. If you want to change the number of posts to display, simply change the value var wk_max_posts = 5; by the one you want.

    Houssen Moshinaly

    To contact the editor personally:

    Leave a Reply

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

    Copy code