我在雨果(Hugo)拥有一个包含大量静态页面和博客的网站。
在首页上,我想创建指向三个最新博客帖子(而不是指向任何可能最近修改的静态页面)的短链接。博客文章都在目录blog/
中。
我无法弄清楚它的语法。到目前为止,我有:
{{- range (.Paginate ( first 3 .Pages.ByDate )).Pages }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{- end}}
,但我还需要按目录blog/
进行过滤。这是在我的layouts/index.html
模板中。
答案 0 :(得分:1)
我正在使用Hugo 0.74.3,这是我的解决方案:
{{ range ( where .Site.RegularPages "Type" "posts" | first 3 ) }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{end}}
请注意,其中不包括draft: true
开头的博客帖子。
我首先迭代.Site.RegularPages
而不用where
来解决
{{ range .Site.RegularPages }}
<h2>{{ . }}</h2>
{{end}}
答案 1 :(得分:0)
Hugo很难使过滤器正常工作,但这可能对您有用
{{ range ( first 3 ( where .Site.Pages "Type" "blog" ).ByDate ) }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}