在Hugo中遍历嵌套部分中的帖子范围。

huangapple go评论80阅读模式
英文:

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 &quot;Section&quot; .Section) &quot;Permalink&quot; &quot;ne&quot; .Permalink }}
  &lt;a href=&quot;{{ .RelPermalink }}&quot;&gt;{{ .LinkTitle }}&lt;/a&gt;
{{ 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 &quot;Permalink&quot; &quot;ne&quot; .Permalink) }}
    &lt;a href=&quot;{{ .RelPermalink }}&quot;&gt;{{ .LinkTitle }}&lt;/a&gt;
{{ end }}

huangapple
  • 本文由 发表于 2022年1月29日 09:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/70901730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定