(HUGO)-链接到第一个/最后一个帖子

Within a Hugo partial I want to have a link to the first and the last post of the section. (Let's call it post)

While I can use an obvious workaround for the first post (just link to /post/000, but that's not satiscfating) the link to the last post is much harder because the current last post changes every time I add a new post.

So: how to do this?

Hugo has a first and last function: https://hugodocs.info/functions/first/ https://hugodocs.info/functions/last/

and you'll have much better luck with questions, and in fact, this question has already been asked, at the official Hugo forum: https://discuss.gohugo.io/

You can do this by filtering the array of all the pages on your site by section, and then choosing the first and last pages in the array. You don't have to sort the list of pages, because they are already sorted by date. Give the following a try (although be warned, I haven't tested it).

{{ $sectionPages := where .Site.Pages "Section" .Section }}
{{ range first 1 $sectionPages }}
  First page title: {{ .Title }}
}}
{{ range last 1 $sectionPages }}
  Last page title: {{ .Title }}
}}