# Range the latest blog post from different sections

**URL:** <https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222>\
**Category:** support\
**Created:** [January 3, 2023, 2:36pm UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222 "2023-01-03T14:36:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tedcm](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tedcm](https://discourse.gohugo.io/u/tedcm)\
**Post date:** [January 3, 2023, 2:36pm UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/1 "2023-01-03T14:36:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 3, 2023, 7:30pm UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/2 "2023-01-03T19:30:47Z")

</div>

```plaintext
{{ $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 }}

```

---

<div class="post-metadata">

**Author:** ![tedcm](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tedcm](https://discourse.gohugo.io/u/tedcm)\
**Post date:** [January 4, 2023, 2:20am UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/3 "2023-01-04T02:20:15Z")

</div>

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.

```auto
{{ 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 }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 4, 2023, 2:39am UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/4 "2023-01-04T02:39:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tedcm](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tedcm](https://discourse.gohugo.io/u/tedcm)\
**Post date:** [January 4, 2023, 2:56am UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/5 "2023-01-04T02:56:53Z")

</div>

I see. thank you for your help and time.

---

<div class="post-metadata">

**Author:** ![tedcm](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@tedcm](https://discourse.gohugo.io/u/tedcm)\
**Post date:** [January 4, 2023, 8:53am UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/6 "2023-01-04T08:53:29Z")

</div>

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

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

```auto
{{ $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 }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 4, 2023, 4:58pm UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/7 "2023-01-04T16:58:52Z")

</div>

You need to nest your `where` clauses.

See docs:  
[https://gohugo.io/functions/where/#nest-where-clauses](https://gohugo.io/functions/where/#nest-where-clauses)

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [January 6, 2023, 4:59pm UTC](https://discourse.gohugo.io/t/range-the-latest-blog-post-from-different-sections/42222/8 "2023-01-06T16:59:29Z")

</div>

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