首帖/末帖的链接

huangapple go评论88阅读模式
英文:

Link to first/last post

问题

在Hugo的部分文件中,我想要添加一个链接到该部分的第一篇最后一篇文章(我们称之为post)。

对于第一篇文章,我可以使用一个明显的解决方法(只需链接到/post/000),但这并不令人满意。然而,链接到最后一篇文章要困难得多,因为每次添加新文章时,最后一篇文章都会发生变化。

所以,如何实现这个功能呢?

英文:

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?

答案1

得分: 1

Hugo有一个firstlast函数:
https://gohugo.io/functions/first/
https://gohugo.io/functions/last/

如果你有更多问题,你可以在Hugo官方论坛上找到答案:https://discuss.gohugo.io/

英文:

Hugo has a first and last function:
https://gohugo.io/functions/first/
https://gohugo.io/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/

答案2

得分: 1

你可以通过按部分筛选您网站上的所有页面的数组,然后选择数组中的第一个和最后一个页面来实现这一点。您不必对页面列表进行排序,因为它们已经按日期排序。尝试以下代码(请注意,我没有测试过):

{{ $sectionPages := where .Site.Pages "Section" .Section }}
{{ range first 1 $sectionPages }}
  第一页标题:{{ .Title }}
}}
{{ range last 1 $sectionPages }}
  最后一页标题:{{ .Title }}
}}
英文:

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 }}
}}

huangapple
  • 本文由 发表于 2017年5月27日 00:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/44206274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定