【问题标题】:How to query site data in hugo using where如何使用 where 在 hugo 中查询站点数据
【发布时间】:2022-06-11 05:56:54
【问题描述】:

在雨果模板中,我如何查找.Site.Data。假设我有这样的数据结构:

{
    "my-blog-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "my-blog-post",
        "Name": "My Blog Post",
        "Parent": "the-parent-post"
    },
    "another-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "another-post",
        "Name": "Another Post",
        "Parent": "the-parent-post"
    }

}

在博客文章的详细信息页面中,我想获取以当前文章为父级的所有文章的列表。基本上我想查询以“父帖子”作为父字段值的帖子。 我可以使用 range 来实现这一点,但我需要让它们使用 where。 我尝试过这样的表达方式,但没有任何乐趣。

{{$child_posts := where (where .Site.Data "Section" "blog") "Parent" "the-parent-post" }}

我可以这样做:

{{$posts := where $site.Pages "Section" "blog"}}
{{ range sort $posts "Name" "asc" }}
    {{ $post := . }}
    {{if eq $post.parent $currentPage.Code}}
          ...Do something...
    {{end}}
{{end}}
    

但这并不理想,也不是我需要的。 提前致谢。

【问题讨论】:

    标签: hugo


    【解决方案1】:

    您正在尝试通过地图的地图进行范围。你需要一组地图。

    https://discourse.gohugo.io/t/filtering-data-in-json-using-where/35287/2

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多