Random post in Jekyll

19 Apr 2013

I’ve been looking for an easy way to get a random post using Jekyll, but I have found nothing. So here is a code snippet that does the trick. Try it: random post

<!-- navbar.html -->
<script>
   var posts = [];
   {% for post in site.posts %}
      posts.push("{{ post.url }}");
   {% endfor %}

   $(function() {
      var goToRandom = function() {}
         window.location = posts[Math.floor(Math.random() * posts.length)];
      }
   });
</script>

<div class="container">
   <div class="row-fluid">
      <div class="navbar">
         <div>
            <a id="random" class="random-post pull-right">
               <i class="icon-random"></i>
            </a>
         </div>
      </div>
   </div>
</div>