[DONE] How to list related content based on section?

I’ve read the documentation but have a lot of trouble listing related posts from the same section and subsection.

This is my config file:

[related]
    treshold = 1
    includeNewer = true
    toLower = false

    [[related.indices]]
        name = "CurrentSection.Title"
        weight = 75
    [[related.indices]]
        name = "Section.Title"
        weight = 50
    [[related.indices]]
        name = "description"
        weight = 25

And this in my template:

{{ $related := .Site.RegularPages.RelatedIndices . | first 5 }}
Number of related pages: {{ len $related }}.<br>

{{ with $related }}
<h2>Related {{ .Section.Title }} articles</h2>
<ul>
    {{ range . }}
    <li><a href="{{ .Permalink }}">{{ .Title }}</a> {{ .Description }}</li>
    {{ end }}
</ul>
{{ end }}

All this does is unfortunately print that there are 0 related pages.

But where easily digs up 5+ pages from the same section with this code:

{{ $pages := where .Site.RegularPages "CurrentSection.Title" .CurrentSection.Title }}
{{ range $pages }}
    {{ .Title }}<br>
{{ end }}

So the pages are there. It’s just that the related indices don’t contain them. What am I missing? :slight_smile:

2 Likes

I haven’t used the built in related posts yet but I would suggest to first try and print out what {{ $related := .Site.RegularPages.RelatedIndices }} is returning without modifying the output. Does it need to be $.Site...?

Get hugo outputting all related content first. Then worry about filtering it with additional functions

The problem is that I get nothing (empty page collection) with that. For example:

{{ $related := .Site.RegularPages.Related . }}
{{ $related }}

Prints:

Pages(0)

There’s no difference for me if I use $.Site... or .Site..., unfortunately.


Related indices would be built around things like tags or other .Params. not items from a section. To relate items from the same section use .Pages.

If I try to show related posts based on description (a custom .Params front matter variable), I neither get a list of related posts, even with a threshold value of 1.

# Related content settings
[related]
    treshold = 1
    includeNewer = true
    toLower = false

    [[related.indices]]
        name = "description"
        weight = 100

Unfortunately if I try to build a related index on ‘description’, but with pages from the same section, I also get an empty page collection.

{{ $related2 := where (.Site.RegularPages.RelatedIndices . "description") "CurrentSection.Title" .CurrentSection.Title }}

Edit

After a bit more research it turns out that related content works much different than I expected.

If I use the following descriptions for all content files, it works:

This is an example description.
This is an example description.
This is an example description.
This is an example description.
This is an example description.

With this, each page gets 4 related posts.

But if the description is even off with one word, it doesn’t work. For instance, as far as Hugo is concerned, a page with a description front matter variable of

This is some description.

Does not relate to

This is an example description.

It gets worse with longer descriptions. The description front matter variable values below all show up in the related posts:

This is a brief description of an article about Hugo's related content feature, which I'm learning about today.
This is a brief description of an article about Hugo's related content feature, which I'm learning about today.
This is a brief description of an article about Hugo's related content feature, which I'm learning about today.
This is a brief description of an article about Hugo's related content feature, which I'm learning about today.
This is a brief description of an article about Hugo's related content feature, which I'm learning about today.

So if I give my 5 posts the exact same description, they show up as related.

But if I change the description value of …

This is a brief description of an article about Hugo’s related content feature, which I’m learning about today.

to …

This is a short description of an article about Hugo’s related content feature, which I’m learning about tomorrow.

(changed words in bold)

…then Hugo does not consider this a related page anymore. And that’s with a threshold value of 1, even though the sentences are 89% identical (only 2 out of 18 words changed).

Perhaps I use the wrong settings? These are my configuration options:

[related]
    treshold = 1
    includeNewer = true
    toLower = false

    [[related.indices]]
        name = "description"
        weight = 100

Looks like you’re on the right track. Relationships are explicit. Sounds like you’re looking for a search functionality here, rather than actual relationships. I wouldn’t think of using a description for a relationship, rather something more like a descriptive tag.

I was looking for a ‘related posts’ feature based on similarities in descriptions. Not an ‘identical posts’ feature that looks for descriptions that exactly match.

Yeah, I removed the related content features from my theme and now use where. This also works for me. :slight_smile:

In case you need to use Related Content also see this detailed article: https://regisphilibert.com/blog/2018/04/hugo-optmized-relashionships-with-related-content/

Thanks for the thoughtful link, but I already read that article. It’s a good one, but didn’t help me with my situation. :slight_smile:

Yeah, where is not as efficient as related, but seems like it will work for you use case, particularly, I think, with "in" and where.