英文:
Range over posts in nested section in hugo
问题
我使用这段代码来在/content/posts中显示相关的帖子。然而,当我将内容移动到嵌套的部分,比如content/posts/news时,代码就停止工作了,导致其他部分的内容出现。有人能提供解决方案或指导我如何使其工作吗?
{{ range where (where site.RegularPages "Section" .Section) "Permalink" "ne" .Permalink }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
英文:
I use this code to show related posts in the same sections for posts in /content/posts. However, the code stops working when I move the content to a nested section, such as content/posts/news, resulting in content from other sections appearing. Could anyone provide a solution or guide me on how to make this work?
{{ range where (where site.RegularPages "Section" .Section) "Permalink" "ne" .Permalink }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
答案1
得分: 1
尝试使用.CurrentSection.Pages,它比从site.RegularPages中筛选所有结果更适合此情况。
然后,可以使用简单的where语句将当前页面过滤掉。
{{ range (where .CurrentSection.Pages "Permalink" "ne" .Permalink) }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
英文:
Try using .CurrentSection.Pages, which is better fit for this than filtering all the results from site.RegularPages.
Then the current page can also be filtered out with a simple where statement.
{{ range (where .CurrentSection.Pages "Permalink" "ne" .Permalink) }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论