# Limit number of posts in range

**URL:** <https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783>\
**Category:** support\
**Created:** [July 31, 2022, 7:25am UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783 "2022-07-31T07:25:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jo\_shim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jo_shim/32/13059_2.png) [@jo\_shim](https://discourse.gohugo.io/u/jo_shim)\
**Post date:** [July 31, 2022, 7:25am UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/1 "2022-07-31T07:25:14Z")

</div>

How can i limit the number of posts in the loop. like i want to show 10 posts but add a class to the first post only.

---

<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:** [July 31, 2022, 3:22pm UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/2 "2022-07-31T15:22:35Z")

</div>

```auto
{{ range $k, $_ := where site.RegularPages "Type" "post" | first 10 }}
  {{ if $k }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ else }}
    <h2 class="first"><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [July 31, 2022, 5:55pm UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/3 "2022-07-31T17:55:42Z")

</div>

For my own learning: am on my phone so haven’t been able to test this out yet.

But does this if statement work because the 0 key/index is considered falsy?

---

<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:** [July 31, 2022, 5:56pm UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/4 "2022-07-31T17:56:29Z")

</div>

> [@zwbetz](#):
>
> does this if statement work because the 0 key/index is considered falsy

Yes…

---

<div class="post-metadata">

**Author:** ![jo\_shim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jo_shim/32/13059_2.png) [@jo\_shim](https://discourse.gohugo.io/u/jo_shim)\
**Post date:** [August 1, 2022, 2:05am UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/5 "2022-08-01T02:05:24Z")

</div>

```auto
<div class="options ">
 
    {{ $paginator := .Paginate (where .Site.RegularPages "Section" "posts") }}
 
    {{ range $index, $element := $paginator.Pages }}
 
     {{ if (eq $index 0 ) }}
      {{ $.Scratch.Set "classname" "active" }}
     {{ else }}
         {{ $.Scratch.Set "classname" "" }}

     {{ end }}
     <div class="option {{ $.Scratch.Get "classname" | safeHTML }}" style="--optionBackground:url({{.Site.BaseURL }}candidates/{{ .Params.image }});">
         <div class="shadow"></div>
         <div class="label">
            <div class="icon">
             <i class="fal fa-bell"></i>
            </div>
            <a href="{{ .Permalink }}" title="{{ .Params.title }}" >
            <div class="info">
               <div class="main">{{ .Params.title }}</div>
               <div class="sub">{{ .Summary | truncate 50 }}</div>
            </div>
            </a>
         </div>
        </div>
    {{end}}
 </div>

```

this works but i need to limit the number of posts in case i have hundreds of posts.

---

<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:** [August 1, 2022, 3:26pm UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/6 "2022-08-01T15:26:51Z")

</div>

> [@jo\_shim](#):
>
> but i need to limit the number of posts

See my previous comment, specifically the call to the `first` function.

---

<div class="post-metadata">

**Author:** ![jo\_shim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jo_shim/32/13059_2.png) [@jo\_shim](https://discourse.gohugo.io/u/jo_shim)\
**Post date:** [August 2, 2022, 6:22am UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/7 "2022-08-02T06:22:21Z")

</div>

thank you very much … your code works 100%

---

<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:** [August 4, 2022, 6:23am UTC](https://discourse.gohugo.io/t/limit-number-of-posts-in-range/39783/8 "2022-08-04T06:23:18Z")

</div>

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