Randomize related posts in Docpad -
i've installed related plugin docpad , want want display 5 random related posts in sidebar of every blogpost. @ moment have set way in post.html.jade
:
div h4 related posts: each doc in getrelateddocuments().slice(0,5) a(href=doc.url)= doc.title br
so, displays 5 posts, aren't random. how shuffle output of getrelateddocuments()
?
have tried variation on question getting random value array?
i created function in docpad.coffee file implement solution:
getrandomposts: (howmany) -> items = @getcollection('posts').tojson() output = [] = 0 while < howmany doc = items[math.floor(math.random() * items.length)] output.push(doc) i++ return output
you may need step in while loop check if doc value in output array math.floor etc possibly return value used.