I am translating some pages and for those with one tag only, Hugo returns no related pages. My expectation was the indices should fall back to the secondary one? Configure related content
Hugo’s related content does not use a “fallback” logic.
It uses a score system. Each index (tags, categories, etc) has points when values overlap. If a page’s total score is above the threshold, it is related.
If a page has a unique tag, and its category is missing or also unique, then it will get a score of 0, so no related pages to show.
Also a translated tag or category counts as a different value, even if it has the same meaning. So translations will not match unless they share the exact same string.
As I said, the weight is for tags, then categories. So, if Hugo finds no tags, it should fall back to pages in the same category. This is not happening in my case. cc @jmooring
Lets say we have a category foo and a tag bar. All posts have category foo but only a few in foo have the tag bar.
Foo has 10 posts in language A but 5 in language B (meaning 5 are translated).
Bar has 6 posts in language A but only 1 in language B.
Using the related config I shared before, bar in language B returns nil for related posts. I was expecting it to fall back to category foo, not retun nil.
If you do that, if a category or a tag has posts less than $n, it returns nil. It seems like a severe limitation of collections.D . Doing {{- $actualN := cond (lt ($pc | len) $n) ($pc | len) $n }} and replacing $n in the range with $actualN seems to solve the issue and the category fallback works. So basically.
IF (we have fewer posts than we want to show)
THEN use the number of posts we have
ELSE use the number we want to show
(I will use the answer that Claude AI generated since it might be more weighty than what I could come up with.)
"Here’s what I’d expect:
Expected behavior: When n > hi (requesting more items than available), collections.D should return all available items (equivalent to returning indices [0, 1, 2, ..., hi-1]).
Current behavior: Returns nil when n > hi
Why this matters:
The primary use case for collections.D (as shown in the docs) is selecting random pages from a collection:
{{ range collections.D seed $n ($pages | len) }}
In real-world usage with .Related, the number of related pages varies per post. Some posts have 10 related pages, others have 3. Currently, I need this workaround:
This defeats the purpose of having a configurable $n parameter.
Comparison with shuffle:
shuffle handles this gracefully - if you request 6 items from a 3-item collection, you get all 3 items. This is the standard behavior users expect from random selection functions.