【问题标题】:Display 3 most recent blog posts in Hugo (but not other pages)在 Hugo 中显示 3 个最近的博客文章(但不显示其他页面)
【发布时间】:2020-09-23 12:47:25
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: hugo


    【解决方案1】:

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

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

    请注意,标题中带有draft: true 的博客文章不包括在内。


    我只是在没有 where 的情况下迭代 .Site.RegularPages 来解决问题

    {{ range .Site.RegularPages }}
      <h2>{{ . }}</h2>
    {{end}}
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 2015-06-04
        • 2017-07-26
        相关资源
        最近更新 更多