Exclude a category from list.html

I want to exclude one category from the main list.html. I’ve looked at this How to check for 'not in' in if statements? but the answer desn’t work for me. This has to be simple :slight_smile:

This shows all post categories in list.html:

{{ define “main” }}
{{ $paginator := .Paginate ( where .Site.RegularPages “Type” “posts” ) 5 }}
{{.Content}}

{{ range .Paginator.Pages }}
    {{ .Render "summary"}}

{{ end }}

{{ partial “paginator.html” . }}

{{ end }}

But this does not exclude the category “blog”:

{{ define “main” }}
{{ $paginator := .Paginate ( where .Site.RegularPages “Type” “posts” ) 5 }}
{{.Content}}

{{ range .Paginator.Pages }}

{{ if not (in .Params.categories “blog”) }}

    {{ .Render "summary"}}

{{ end }}

{{ end }}

{{ partial “paginator.html” . }}

{{ end }}

How do I exclude the category “blog” from list.html?

Hello my friend, how do you do?

Perhaps you can use something like this:

{{ range $i, $e := where ($paginator.Pages) "Section" "!=" "blog" }}

or in your case:

{{ define "main" }}
{{ $paginator := .Paginate ( where .Site.RegularPages "Type" "posts" ) 5 }}
{{.Content}}

{{ range $i, $e := where ($paginator.Pages) "Section" "!=" "blog" }}
    {{ .Render "summary"}}

{{ end }}
{{ partial “paginator.html” . }}

{{ end }}

Please consult: https://gohugo.io/functions/where/

Thanks, but that doesn’t want to work. No errors, just I still see category “blog” posts, even after a Hugo restart.

Perhaps instead of .Site.RegularPages you can try .Pages instead?

.Site.RegularPages
a shortcut to the regular page collection. .Site.RegularPages is equivalent to where .Site.Pages “Kind” “page”.

Specifically:

The .Pages Variable
.Pages is an alias to .Data.Pages. It is conventional to use the aliased form .Pages.

.Pages compared to .Site.Pages
A regular page is a “post” page or a “content” page.
A leaf bundle is a regular page.
A list page can list regular pages and other list pages. Some examples are: homepage, section pages, taxonomy term (/tags/) and taxonomy (/tags/foo/) pages.
A branch bundle is a list page.

From: https://gohugo.io/variables/page/#pages

That still doesn’t work. But I kept looking around and found that
{{ if not (in .Params.categories "Blog") }}
works.

Glad to hear. This is what I use myself

{{ $paginator := .Paginate (where .Data.Pages "Type" "in" .Site.Params.mainSections) }}
{{ range $i, $e := where ($paginator.Pages) "Section" "!=" "blog" }}

and site config.toml has:

[params]
    mainSections = ["blog", "news"]

Ok, I don’t use the [params] for mainSections in config.toml.

Well, what you’re essentially doing is checking the page’s front matter for categories = [“something”, “something else”] which may not be the best to depend on front matter. Instead, try to filter out the pages using the project directory/file structure, such as using .Pages not from section Blog.

Anyway, glad that works for you.

Ok, I got started with using categories in front matter from a theme I decided to use; it was set up that way. Maybe that’s not a good way, i.e. my newer question? Custom template for one category?

Oh right, my mistake. I forgot categories are not sections. Doh!

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