Range the latest blog post from different sections

I’m trying to get the latest post in each section with this code:

{{ range first 1 (where .Site.Pages "Section" "in" (slice "section-1" "section-2" "section-3")) }}

But it only ranges one post.

If I change it to 3, it doesn’t work as well.

{{ $p := slice }}
{{ range (slice "articles" "books" "posts") }}
  {{ $p = $p | append (index (where site.RegularPages "Section" . | first 1) 0) }}
{{ end }}

{{ range $p.ByDate }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}
3 Likes

Thank you! You made my day.

I’ve modified it a bit to render contents and

I’m trying to enable the toc but it doesn’t seem to recognize the h2 in the contents.

{{ define "main" }}
  <main id="main">

    <h1>
      <svg class="bookmark-icon" aria-hidden="true" viewBox="0 0 40 50" focusable="false">
        <use href="#bookmark"></use>
      </svg>
      {{ .Title }}
    </h1>

    <div class="date">
      {{ $dateFormat := $.Site.Params.dateFormat | default "Jan 2, 2006" }}
      {{ $publishDate := .PublishDate }}
      <strong>{{ T "publish_date" }} </strong>{{ $publishDate.Format $dateFormat }}
      {{ with .Lastmod }}
        {{ if gt . $publishDate }}
          <br>
          <strong>{{ T "last_updated" }} </strong>{{ .Format $dateFormat }}
        {{ end }}
      {{ end }}
    </div>

    {{ with .Params.tags }}
      <div class="tags">
        <strong>{{ T "tags" }} </strong>
        <ul aria-label="{{ T "aria_label_tags" }}">
          {{ range . }}
            <li>
              <svg class="tag-icon" aria-hidden="true" viewBox="0 0 177.16535 177.16535" focusable="false">
                <use href="#tag"></use>
              </svg>
              {{ $href := print ("tags/" | relLangURL) (. | urlize) "/" }}
              <a href="{{ $href }}">{{ . }}</a>
            </li>
          {{ end }}
        </ul>
      </div>
    {{ end }}

    {{ partial "toc.html" . }}

    {{ $p := slice }}
    {{ range (slice "articles" "books" "posts") }}
      {{ $p = $p | append (index (where site.RegularPages "Section" . | first 1) 0) }}
    {{ end }}
    
    {{ range $p.ByDate }}
      {{ .Content }}
    {{ end }}
  </main>
{{ end }}

The .TableOfContents for a single page is built from the markdown in the content file (e.g., content/posts/post-1.md).

The .TableOfContents for a list page is built from the markdown in the content file (e.g., content/posts/_index.md).

The rendered .Content from the pages you are ranging over will never be part of the TOC.

I see. thank you for your help and time.

1 Like

Can you add the ability to range only the ones that are dated today.

I’ve tried this but it doesn’t work.

{{ $p := slice }}
{{ range (slice "articles" "books" "posts") }}
  {{ $p = $p | append (where site.RegularPages "Section" . "Date" (time.Now.Format "2006-01-02") | first 1) 0) }}
{{ end }}

{{ range $p.ByDate }}
  <h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3>
{{ end }}

You need to nest your where clauses.

See docs:
https://gohugo.io/functions/where/#nest-where-clauses

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.