How to add a list of recent posts to each post


#1

Hi - newbie alert - I’m creating a new theme for my weblog.

The home page has only the 3 most recent posts. Each one links to the post itself, i.e. a “single.html” template. Now I’d like to include a longer list of recent posts as sidebar - how is this done in Hugo? I guess I’m a bit confused w.r.t. the single / list template split: it’s a single page in that it shows one post in full, but it needs to include a list of the last N posts as well.

Apologies if this is a FAQ - I haven’t found an answer or matching example so far.

-jcw

Update - using “.Site.Pages” iso “.Data.Pages” with “where” filtering got me going (as mentioned here). Not sure it’s the “proper” way to do this, but it works.


#2

Also have a look at the first function. (and after that, maybe the related first and last function). Combined with where they are very powerful.


#3

Yes - thanks, first and where did the trick.


#5

But alas no code sample to see how they were applied…

update for all those spending their afternoon tyring to create a list of recent posts on a single piece of content… this worked for me.

      {{ range where .Site.Pages "Section" "blog" }}
        <li><a href="{{ .Permalink }}">{{ .Title }}</a><br /><span class="date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span></li>
        {{ end }}

Can someone explain to me why this code below only worked on a list view and return only blank on single view?

      {{ range .Data.Pages.ByDate }}
        <li><a href="{{ .Permalink }}">{{ .Title }}</a><br /><span class="date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span></li>
        {{ end }}

Clearly .Site.Pages is being filtered… I guess I still don’t understand the difference between .Site.Pages and .Data.Pages… and why .Data.Pages doesn’t work on single views…


#6

Unfortunately it does not work when I put in partials/footer.html. I get error:

ERROR 2017/12/05 16:53:06 Error while rendering "home": template: index.html:149:3: executing "index.html" at <partial "footer">: error calling partial: template: partials/footer.html:47:46: executing "partials/footer.html" at <where .Site.Pages "S...>: error calling where: can't iterate over <nil>

but it works if I put it directly somewhere in layouts/index.html. How can I add 4 recent posts in the footer?