如何在Hugo中创建一个用于渲染永久链接的页面

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

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 = &quot;/:year/:month/:title/&quot;

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

huangapple
  • 本文由 发表于 2016年12月24日 08:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/41309310.html
匿名

发表评论

匿名网友

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

确定