Related Posts by Section

Is it possible to have related posts by section only? I want the related posts in each section to only pull related posts from the same section. I don’t have taxonomies defined coz I have foregone them. (I saw this topic and this topic but it was never resolved)

In the project config you can assign custom parameters per section in the Related Content config, for example:

[related]
  includeNewer = false
  threshold = 80
  toLower = false
[[related.indices]]
  name = "foo"
  weight = 100
[[related.indices]]
  name = "bar"
  weight = 100

And then (again Front Matter Cascade is configured in the project config):

[[cascade]]
  foo = true
[cascade._target]
  kind = "page"
  path = "/foo/**"
[[cascade]]
  bar = true
[cascade._target]
  kind = "page"
  path = "/bar/**"

With the above configuration you should have related content per Section.
i.e. under /bar/only related content with bar = true should be displayed and so on.

There may be a simpler approach.

If you just want to list the other pages in the current section, place this in a single.html template:

{{ range where (where site.RegularPages "Section" .Section) "Permalink" "ne" .Permalink }}
  <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}

If you have defined related content in your site configuration, and you only wish to display related pages within the current section, place this in a single.html template:

{{ range where (site.RegularPages.Related .) "Section" .Section }}
  <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
6 Likes

This did the trick. I was avoiding having to use front matter variables at all costs.

1 Like

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