How can I exclude certain categories from the homepage?

Hello everyone! I am in the process of migrating my blog from WordPress to Hugo, and I’ve encountered an issue.
I am using the hugo-theme-stack theme, and I want to exclude articles from two categories from appearing on the homepage. I have modified the layouts/index.html, but it doesn’t seem to be working. I also tried modifying the layouts/_default/list.html , but that didn’t work either.I have searched for a long time on the forum, but I haven’t found a suitable method.

layouts/index.html

   {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
    {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }}
    {{ $filtered := ($pages | intersect $notHidden) }}
    {{ $pag := .Paginate ($filtered) }}

before

    {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
    {{ $notHidden := where $pages "Params.hidden" "!=" true }}
    {{ $filtered := where $notHidden ".Params.categories" "intersect" (slice "moments" "thinks") }}
    {{ $pag := .Paginate ($filtered) }}

modifiled

thx!

  {{ $p1 := where site.RegularPages "Type" "in" site.Params.mainSections }}
  {{ $p2 := where $p1 "Params.categories" "intersect" (slice "moments" "thinks") }}
  {{ $p := $p1 | complement $p2 }}

Think of the last line in terms of subtraction. $p = $p1 - $p2.

Thank you for your assistance. Since I am not proficient in Go language, following your guidance, I used 3 method, but it still doesn’t work. The homepage continues to display all the articles.

method 1

{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where $pages "Params.hidden" "!=" true }}
{{ $filtered := where $notHidden ".Params.categories" "intersect" (slice  "moments" "thinks") }}
{{ $filteredPages := where $notHidden "Page" "not in" $filtered }}
{{ $pag := .Paginate ($filteredPages) }}

method 2

{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where $pages "Params.hidden" "!=" true }}
{{ $intersection := intersect $pages $notHidden }}
{{ $filteredPages := slice }}
{{ range $page := $intersection }}
    {{ if intersect $page.Params.categories (slice "moments" "thinks") }}
        {{ $filteredPages = $filteredPages | complement $page }}
    {{ end }}
{{ end }}
{{ $pag := .Paginate ($filteredPages) }}

method 3

{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $notHidden := where $pages "Params.hidden" "!=" true }}
{{ $filtered := where $notHidden ".Params.categories" "intersect" (slice  "moments" "thinks") }}
{{ $pag := .Paginate $filtered }}

I suggest you follow my earlier guidance. It works great. Try it:

git clone --single-branch -b hugo-forum-topic-50046 https://github.com/jmooring/hugo-testing hugo-forum-topic-50046
cd hugo-forum-topic-50046
hugo server

Files of interest:

  • layouts/_default/home.html (lines 13-21)

Also, when posting code, configuration, or data on this forum, please wrap the text within tripple backticks or use the </> button in the menu.

```
my code
```
1 Like

I am sorry, in fact, I had copied your code earlier, but it did not work. After reviewing your test code, I even copied the entire home.html to my index.html, but it still display all the articles.

I tried creating a /content/page/test/index.md, and added a test.html under layouts/page, and copied your code into it, it works well.

test.html

    {{ $p1 := where site.RegularPages "Type" "in" site.Params.mainSections }}
    {{ $p2 := where $p1 "Params.categories" "intersect" (slice "moments" "thinks") }}
    {{ $p := $p1 | complement $p2 }}

    <div class="article-list--compact">
        {{ range $p }}
            {{ partial "article-list/compact" . }}
        {{ end }}
    </div>

I am wondering if this issue is caused by the special settings of the theme, but I haven’t received a response yet after submitting an issue on GitHub.

Just now, I found the source of the problem. The hugo-theme-stack sets another file to control the display of articles on the homepage, located at layouts/partials/data/title.html. By modifying this file, I have achieved my goal.

Thank you once again for your help; it was my carelessness when searching for the function earlier that caused me to overlook it. When I re-searching for {{ $pag := .Paginate ($filtered) }} that I discovered this code also existed in another file.

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