首页 > 解决方案 > Hugo 无法将 page.PagesGroup 类型转换为 Pages

问题描述

我有一个这样的雨果列表模板:

{{ range (.Paginate (.Data.Pages.GroupByDate "2006")).PageGroups  }}

<h3>{{ .Key }}</h3>
<ul>
    {{ range .Pages.ByWeight }}
    <li>
        <a href="{{ .Permalink }}">{{ if .Draft }}{{ T "draft" }}: {{end}}{{ .Title | markdownify }}</a>
        <time class="date-meta">{{ .Date.Format "Jan 2" }}</time>
    </li>
    {{ end }}
</ul>

{{ end }}

当我像这样运行网站时,hugo server -D它工作正常。

当我建立网站时,我得到:

执行模板失败:模板:_default/list.html:15:14:在 <.Paginate> 处执行“main”:调用分页时出错:无法将 page.PagesGroup 类型转换为 Pages

打开调试和详细没有帮助。我有:

content
content/web
content/web/one.md
content/web/two.md
content/web/_index.md
content/web/three.md
content/about
content/about/index.md

是什么赋予了?

标签: hugo

解决方案


我在编写自定义主题时遇到了这个错误,除非它发生在运行hugo -Dhugo server -D. 作为一种解决方法,尝试将其包装在一个if条件中以检查.Data.Pages.

{{ if .Data.Pages }}
    {{ range (.Paginate (.Data.Pages.GroupByDate "2006")).PageGroups  }}
    ...
    {{ end }}
{{ end }}

推荐阅读