渲染 Hugo 中特定类别的帖子

发布于 2025-01-15 13:34:45 字数 548 浏览 4 评论 0原文

我从 Hugo 开始,但没有找到好的定制示例。 我需要渲染名为“视频”的类别中的所有帖子,以在模板“list.html”内的主站点上显示。

如何更改 term.html 模板中的代码以仅处理这个单一类别?任何帮助将不胜感激!

<div class="container">
<article>
<main>
<h1>{{ $.Scratch.Get "Title" }}</h1>

<ul class="terms">
  {{ range $key, $value := .Data.Terms }}
  <li>
    <a href="{{ (print "/" $.Data.Plural "/" $key) | relURL }}">
      {{ $key }}
    </a>
    ({{ len $value }})
  </li>
  {{ end }}
</ul>
</main>
</article>
</div>

I started with Hugo but dont find good examples for customization.
I need to render all posts in a category called e.g. "video" to show on main site within a template "list.html".

How to change the code from term.html template to adress only this single category? Any help would be deeply appreciated!

<div class="container">
<article>
<main>
<h1>{{ $.Scratch.Get "Title" }}</h1>

<ul class="terms">
  {{ range $key, $value := .Data.Terms }}
  <li>
    <a href="{{ (print "/" $.Data.Plural "/" $key) | relURL }}">
      {{ $key }}
    </a>
    ({{ len $value }})
  </li>
  {{ end }}
</ul>
</main>
</article>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-01-22 13:34:45

我根据主题中可用的 list.html 为名为“cats”的类别开发了以下代码:

{{ $var1 := "cats"}}
{{ range $key, $value := .Site.Taxonomies.categories }}
{{ if eq $key $var1  }}


{{ range $value.Pages }}
<section class="article-list">
  <div class="categories">
    {{ with .Params.categories }}
    {{ range first 1 . }}
      <a href="{{ relURL (print "/categories/" . | urlize) }}">{{ . }}</a>
    {{ end }}
    {{ else }}
      <a>{{ default "Uncategorized" .Site.Params.text.uncategorized }}</a>
    {{ end }}
  </div>

  <h1><a href="{{ .RelPermalink }}">{{ .Title }}</a></h1>

  <div class="summary">
      <a href="{{ .RelPermalink }}">
      {{ with .Resources.GetMatch (printf "%s" .Params.thumbnail) }}
      <div class="thumbnail"><img src="{{ relURL .Permalink }}" alt="Thumbnail" /></div>
      {{ else }}
      {{ with  .Params.thumbnail }}
      <div class="thumbnail"><img src="{{ relURL .}}" alt="Thumbnail" /></div>
      {{ else }}
      {{ $img := findRE "<img src=\"[^\"]+\"" .Content 1 }}
      {{ range $img }}
      <div class="thumbnail">{{ (print . " alt=\"Thumbnail\" />") | safeHTML }}</div>
      {{ end }}
      {{ end }}
      {{ end }}
      {{ with .Description }}
        {{ $.Scratch.Set "summary" (markdownify .) }}
      {{ else }}
        {{ $.Scratch.Set "summary" ((delimit (findRE "(<p.*?>(.|\n)*?</p>\\s*)+" .Content) "[…] ") | plainify | truncate (default 200 .Site.Params.summary_length) (default " …" .Site.Params.text.truncated ) | replaceRE "&" "&" | safeHTML) }}
      {{ end }}
      {{ $.Scratch.Get "summary" }}
      </a>
  </div>
</section>
{{ end }}

{{ end }}
{{ end }}

如果它位于 list.html 中,则它仅显示此特定类别中的帖子。或者可以将其放入单独的部分模板 cats.html 中

I developed following code for category called "cats" based on list.html available in my theme:

{{ $var1 := "cats"}}
{{ range $key, $value := .Site.Taxonomies.categories }}
{{ if eq $key $var1  }}


{{ range $value.Pages }}
<section class="article-list">
  <div class="categories">
    {{ with .Params.categories }}
    {{ range first 1 . }}
      <a href="{{ relURL (print "/categories/" . | urlize) }}">{{ . }}</a>
    {{ end }}
    {{ else }}
      <a>{{ default "Uncategorized" .Site.Params.text.uncategorized }}</a>
    {{ end }}
  </div>

  <h1><a href="{{ .RelPermalink }}">{{ .Title }}</a></h1>

  <div class="summary">
      <a href="{{ .RelPermalink }}">
      {{ with .Resources.GetMatch (printf "%s" .Params.thumbnail) }}
      <div class="thumbnail"><img src="{{ relURL .Permalink }}" alt="Thumbnail" /></div>
      {{ else }}
      {{ with  .Params.thumbnail }}
      <div class="thumbnail"><img src="{{ relURL .}}" alt="Thumbnail" /></div>
      {{ else }}
      {{ $img := findRE "<img src=\"[^\"]+\"" .Content 1 }}
      {{ range $img }}
      <div class="thumbnail">{{ (print . " alt=\"Thumbnail\" />") | safeHTML }}</div>
      {{ end }}
      {{ end }}
      {{ end }}
      {{ with .Description }}
        {{ $.Scratch.Set "summary" (markdownify .) }}
      {{ else }}
        {{ $.Scratch.Set "summary" ((delimit (findRE "(<p.*?>(.|\n)*?</p>\\s*)+" .Content) "[…] ") | plainify | truncate (default 200 .Site.Params.summary_length) (default " …" .Site.Params.text.truncated ) | replaceRE "&" "&" | safeHTML) }}
      {{ end }}
      {{ $.Scratch.Get "summary" }}
      </a>
  </div>
</section>
{{ end }}

{{ end }}
{{ end }}

If it is in list.html, it shows only posts in this particular category. Or it can be put in a separate partial template cats.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文