显示 Hugo 中最新的 3 篇博客文章(但不包括其他页面)

Joh*_*nst 7 hugo

我在 Hugo 中有一个包含一堆静态页面和一个博客的网站。

在首页上,我想创建指向最近三篇博客文章的短链接(但不是任何可能最近修改的静态页面)。博客文章都在目录中blog/

我无法弄清楚这个的语法。到目前为止,我有:

{{- range (.Paginate ( first 3 .Pages.ByDate )).Pages }}
    <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{- end}}
Run Code Online (Sandbox Code Playgroud)

但我还需要按目录过滤blog/。这是我的layouts/index.html模板中的。

Fel*_*Dev 7

我正在使用 Hugo 0.74.3,这是我的解决方案:

{{ range ( where .Site.RegularPages "Type" "posts" | first 3 ) }}
  <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{end}}
Run Code Online (Sandbox Code Playgroud)

draft: true请注意,不包括在 frontmatter 中包含的博客文章。


我一开始只是迭代.Site.RegularPages而不去where弄清楚

{{ range .Site.RegularPages }}
  <h2>{{ . }}</h2>
{{end}}
Run Code Online (Sandbox Code Playgroud)


Bee*_*ler 1

Hugo 很难让过滤工作,但这可能对你有用

{{ range ( first 3 ( where .Site.Pages "Type" "blog" ).ByDate ) }}
  <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
Run Code Online (Sandbox Code Playgroud)