英文:
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有一个first
和last
函数:
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 }}
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论