英文:
How to create a page to render permalink in Hugo
问题
根据你提供的信息,你想在Hugo中实现按日期或年份显示文章列表的功能。你可以通过使用Hugo的taxonomies功能来实现这个目标。
首先,在你的配置文件中添加以下内容:
[taxonomies]
year = "years"
month = "months"
然后,在你的文章的front matter中添加以下内容:
---
title: "My Post"
date: 2016-12-01
year: 2016
month: 12
---
接下来,你可以创建一个模板来显示按日期或年份的文章列表。例如,你可以创建一个名为year.html
的模板来显示按年份的文章列表,内容如下:
<h1>Posts published in {{ .Title }}</h1>
<ul>
{{ range where .Site.RegularPages "Params.year" .Title }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
类似地,你可以创建一个名为month.html
的模板来显示按月份的文章列表。
最后,你可以通过访问http://example.com/2016/
来查看2016年发布的文章列表,或者访问http://example.com/2016/12/
来查看2016年12月发布的文章列表。
希望这可以帮助到你!
英文:
I followed this tutorial in Hugo and its working fine. Basically I have the following content:
- content
`- post
`- coding
`- html
`- my-post.md
and my config file set to
[permalinks]
post = "/:year/:month/:title/"
which gives me the URL
http://example.com/2016/12/my-post/
What I want is for readers to see a list of post based on a date or year. For example if they visit http://example.com/2016/12/
they will see a list of post published in December. If they visit http://example.com/2016/
they will see a list of post published in 2016.
Is there any way to do that in Hugo?
答案1
得分: 1
这个最简单的方法是在Hugo论坛上找到的,答案是为每一年使用一个分类法。每篇文章都需要放置在该年份的分类法中,并使用自定义的列表模板来生成页面。这样Hugo就可以为每个“年份”(分类法)创建一个索引页面,其中条目是“月份”。这是一个创造性的解决方案,尽管比人们希望的更繁琐。
https://discuss.gohugo.io/t/pagination-and-group-by-date/1441
英文:
The easiest way to do this was found on the Hugo forums, and the answer is to use a Taxonomy for every year. Each post needs to be placed in to that year's taxonomy and a custom listing template is used to generate the pages. This will allow Hugo to create an index page for each "year" (taxonomy) where the entries are the "month." It's a creative solution, although more cumbersome than one would like.
https://discuss.gohugo.io/t/pagination-and-group-by-date/1441
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论