# Exclude pages by "type" from 'NextInSection'

**URL:** <https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259>\
**Category:** support\
**Created:** [February 4, 2020, 5:34pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259 "2020-02-04T17:34:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ominty](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ominty/32/2716_2.png) [@ominty](https://discourse.gohugo.io/u/ominty)\
**Post date:** [February 4, 2020, 5:34pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259/1 "2020-02-04T17:34:05Z")

</div>

I use something like this to turn pages, in a partial:

```
<div class="screen">
    <ul class="umblaettern">
        <li>{{if .PrevInSection}}
            <a href="{{.PrevInSection.RelPermalink}}"><<< next article</a>
            {{end}}</li>
        <li>{{if .NextInSection}}
            <a href="{{.NextInSection.RelPermalink}}">previous article >>></a>
            {{end}}</li>
    </ul>
</div>

```

Now I have pages marked by a page parameter and these pages should be excluded.  
Page frontmatter:

```
type = "teaser"

```

I can’t find a way to do that, i. e.

```
        <li>{{if .NextInSection}}
            {{ if ne "Type" "teaser" }}
            <a href="{{.NextInSection.RelPermalink}}">next article >>></a>
            {{end}}
            {{end}}</li>

```

does not show any effect, neither does

```
            {{ if ne .Params.Type "teaser" }}

```

instead.

Obviously I have to examine, if the front matter of the _next_ page contains the “type” frontmatter, but my partial seems to examine the _current_ page …

Is there a way to do this right?

---

<div class="post-metadata">

**Author:** ![setphen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/setphen/32/10385_2.png) [@setphen](https://discourse.gohugo.io/u/setphen)\
**Post date:** [February 4, 2020, 6:12pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259/2 "2020-02-04T18:12:49Z")

</div>

If you want the next page in the section which does not have a certain value for a parameter, you may need to use a recursive partial. This is because `.NextInSection` is not a collection that you can filter, but just a single page.

Here’s an example of how you could create a recursive partial

```auto
// in the base template

{{ partial "nextpage" . }}

```

```auto
// in /layouts/partials/nextpage.html

{{ with .NextInSection }}
  // Check if the next page is a teaser
  {{ if eq .Params.Type "teaser" }}
    // if so, try the next next page (this creates recursion)
    {{ partial "nextpage" . }}
  {{ else }}
    // otherwise render the next page link
    <a href="{{ .RelPermalink }}"> Next Page </a>
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [February 4, 2020, 8:26pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259/3 "2020-02-04T20:26:24Z")

</div>

You can filter using `where` and then

> **[Pages Methods](https://gohugo.io/variables/pages/#next-page)**
>
> Pages is the core page collection in Hugo and has many useful methods.

---

<div class="post-metadata">

**Author:** ![ominty](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ominty/32/2716_2.png) [@ominty](https://discourse.gohugo.io/u/ominty)\
**Post date:** [February 16, 2020, 9:41pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259/4 "2020-02-16T21:41:25Z")

</div>

> [@setphen](#):
>
> If you want the next page in the section which does not have a certain value for a parameter, you may need to use a recursive partial. This is because `.NextInSection` is not a collection that you can filter, but just a single page.

Thanks for this - I got it to work easily - and as my teaser pages are “at the end” of the list because of hugos default sorting by weight/date/name (teaser pages having associated weight - other pages in the blog section do not) I could do without the recursion in this case

> [@bep](#):
>
> You can filter using `where` and then

I tried many things to get this to work - no success.

```
{{ range .Site.Pages }}

```

gives all pages, not only the ones of the blog section (as expected).

```
{{ range .Data.Pages }}

```

doesn’t give any pages …

I then tried to do something like - in words “range .Site.Pages _where_ section blog _and_ type _ne_ ‘teaser’” in many different ways but hugo kept complaining about wrong number of arguments etc. … til I gave up.

---

<div class="post-metadata">

**Author:** ![brianwisti](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brianwisti/32/119_2.png) [@brianwisti](https://discourse.gohugo.io/u/brianwisti)\
**Post date:** [June 10, 2020, 7:21pm UTC](https://discourse.gohugo.io/t/exclude-pages-by-type-from-nextinsection/23259/5 "2020-06-10T19:21:30Z")

</div>

Adding to the thread because @bep’s reply got me halfway to solving a similar `.Next` confusion.

First, build the collection of pages you care about with nested `where` filters:

```auto
{{ $pages := where (where .Site.RegularPages "Section" .Section) "Type" "!=" "teaser" }}

```

Then, use that collection’s `.Next` and `.Prev` methods with the current context:

```auto
{{ with $pages.Prev . }} ... {{ end }}
{{ with $pages.Next . }} ... {{ end }}

```

This way we don’t have to care about recursive partials.
