Order by Random?

63 views
Skip to first unread message

loads...@gmail.com

unread,
Sep 3, 2014, 12:26:49 AM9/3/14
to hugo-d...@googlegroups.com
Does anyone know any way to display random content, so that each page refresh would render new content? {{ range random }} style.

I'm pulling the title and links of 5 pieces of content to the homepage, but the website is not date-defined and I'd like to show any 5 posts, and not the same 5 posts every time.

Nate Finch

unread,
Sep 3, 2014, 9:18:50 AM9/3/14
to loads...@gmail.com, hugo-d...@googlegroups.com
You can't really do that with Hugo.... remember, this is all static pages that are rendered once and then just served up as-is. 

In order to do something like that, you'd need to have all the content rendered on the webpage, and then use javascript to show or hide some of it at display time. It's not impossible, but it would be a pain in the butt, and would mean the browser is loading all the content every time, even if only 5 are showing.

Alternatively, you could embed data from a different server that is dynamic (this is how disqus comments work, and how you could do something like showing your twitter feed in the sidebar).  But that's beyond the scope of Hugo.


On Wed, Sep 3, 2014 at 12:26 AM, <loads...@gmail.com> wrote:
Does anyone know any way to display random content, so that each page refresh would render new content? {{ range random }} style.

I'm pulling the title and links of 5 pieces of content to the homepage, but the website is not date-defined and I'd like to show any 5 posts, and not the same 5 posts every time.

--
Hugo
 
A Fast and Flexible Static Site Generator built with love by spf13 and friends in Go
 
Download the latest at https://github.com/spf13/hugo/releases
---
You received this message because you are subscribed to the Google Groups "Hugo Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hugo-discuss...@googlegroups.com.
To post to this group, send email to hugo-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/hugo-discuss.
For more options, visit https://groups.google.com/d/optout.

loads...@gmail.com

unread,
Sep 3, 2014, 10:51:15 AM9/3/14
to hugo-d...@googlegroups.com, loads...@gmail.com

Thanks for that Nate, and the explanation. 

Simon Griffee

unread,
Jun 27, 2016, 8:11:01 PM6/27/16
to Hugo Discuss
1. Create a folder called random in your content folder and put a file called index.md inside content/random with the following code:

---
title
: Redirecting to a random entry
type
: random
---  



2. Create a folder called random in your layouts folder and put a file called single.html inside layouts/random with the following code:

<html>
<head>
   
<script>
   
// https://github.com/rwmpelstilzchen/jekyll-random-post
       
var posts = [];
   
{{ range .Site.Pages }}
      posts
.push("{{ .URL }}");
   
{{ end }}

        document
.write("<meta http-equiv=\"refresh\" content=\"1;url=");
        document
.write(posts[Math.floor(Math.random() * posts.length)]);
        document
.write("\">");
   
</script>
</head>

<body>
    Redirecting to a
   
<script>
        document
.write("<a href=\"");
        document
.write(posts[Math.floor(Math.random() * posts.length)]);
        document
.write("\">random post</a>...");
   
</script>
</body>
</html>

3. Put <a href="/random/">Random</a> somewhere on your site's templates, load the site and click the link. Boom!

Thanks to https://github.com/rwmpelstilzchen/jekyll-random-post
Reply all
Reply to author
Forward
0 new messages