Exclude subsection from index page

I want to create a JSON and an index page that excludes posts from one subsection. I have the subsections inside the posts folder in content. I have tried different examples in this forum including this one and this one but none works.

Edit: I am using front matter variables for now as a workaround. But I would prefer if I could do this at the subsection level without relying on front matter.

Code / context? What specifically did you try code wise (your code, not what you followed)? What happened? What did you expect to happen?

{{ range where site.RegularPages "Section" "!=" "one-subsection" }}
<a href="{{ .Permalink }}">{{ .Title }}</a>
{{ end }}

This works for sections e.g. posts in content but not for subsections, e.g. inside content, posts, subsection.

The way I would go about it, would be to first call the Parent section in the list
e.g. {{ if in .Section "parent" }}

and then within the above context use something like:

site.RegularPages "Sections" "!=" "one-subsection"

.Sections
The sections below this content.

Also see Section Variables and Methods

I think what you need is .CurrentSection. From the docs:

.CurrentSection

The page’s current section. The value can be the page itself if it is a section or the homepage.

compared to:

.Section

The section this content belongs to. Note: For nested sections, this is the first path element in the directory, for example, /blog/funny/mypost/ => blog .

EDIT: But I don’t used nested sections, so I make no promises…

Your solution worked for my index page Your solution returns blank in my JSON and index file.

You also need to include it in the relevant template for the JSON output format.

In any case as @cshoredaniel mentioned above you may need to share a sample of the project that reproduces the setup, so that we can see where the problem lies.

Here you go. Also, add /index.json to your local URL to test the JSON output. I have no JSON template. I use the code with JS in a content page.