Debian Codegraph @

docs/content/en/content-management/related-content.md

raw
1 ---
2 title: Related content
3 description: List related content in "See Also" sections.
4 categories: []
5 keywords: []
6 aliases: [/content/related/,/related/,/content-management/related/]
7 ---
8
9 Hugo uses a set of factors to identify a page's related content based on front matter parameters. This can be tuned to the desired set of indices and parameters.
10
11 ## List related content
12
13 To list up to 5 related pages (which share the same `date` or `keyword` parameters), include something similar to this _partial_ template:
14
15 ```go-html-template {file="layouts/_partials/related.html" copy=true}
16 {{ with site.RegularPages.Related . | first 5 }}
17 <p>Related content:</p>
18 <ul>
19 {{ range . }}
20 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
21 {{ end }}
22 </ul>
23 {{ end }}
24 ```
25
26 The `Related` method takes one argument which may be a `Page` or an options map. The options map has these options:
27
28 `indices`
29 : (`slice`) The indices to search within.
30
31 `document`
32 : (`page`) The page for which to find related content. Required when specifying an options map.
33
34 `namedSlices`
35 : (`slice`) The keywords to search for, expressed as a slice of `KeyValues` using the [`keyVals`][] function.
36
37 `fragments`
38 : (`slice`) A list of special keywords that is used for indices configured as type "fragments". This will match the [fragment](g) identifiers of the documents.
39
40 A fictional example using all of the above options:
41
42 ```go-html-template
43 {{ $page := . }}
44 {{ $opts := dict
45 "indices" (slice "tags" "keywords")
46 "document" $page
47 "namedSlices" (slice (keyVals "tags" "hugo" "rocks") (keyVals "date" $page.Date))
48 "fragments" (slice "heading-1" "heading-2")
49 }}
50 ```
51
52 > [!NOTE]
53 > We improved and simplified this feature in Hugo 0.111.0. Before this we had 3 different methods: `Related`, `RelatedTo` and `RelatedIndices`. Now we have only one method: `Related`. The old methods are still available but deprecated. Also see [this blog article][] for a great explanation of more advanced usage of this feature.
54
55 ## Index content headings
56
57 Hugo can index the headings in your content and use this to find related content. You can enable this by adding a index of type `fragments` to your `related` configuration:
58
59 {{< code-toggle file=hugo >}}
60 [related]
61 threshold = 20
62 includeNewer = true
63 toLower = false
64 [[related.indices]]
65 name = 'fragmentrefs'
66 type = 'fragments'
67 applyFilter = true
68 weight = 80
69 {{< /code-toggle >}}
70
71 - The `name` maps to a optional front matter slice attribute that can be used to link from the page level down to the fragment/heading level.
72 - If `applyFilter` is enabled, the `.HeadingsFiltered` on each page in the result will reflect the filtered headings. This is useful if you want to show the headings in the related content listing:
73
74 ```go-html-template
75 {{ $related := .Site.RegularPages.Related . | first 5 }}
76 {{ with $related }}
77 <h2>See Also</h2>
78 <ul>
79 {{ range $i, $p := . }}
80 <li>
81 <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
82 {{ with .HeadingsFiltered }}
83 <ul>
84 {{ range . }}
85 {{ $link := printf "%s#%s" $p.RelPermalink .ID | safeURL }}
86 <li>
87 <a href="{{ $link }}">{{ .Title }}</a>
88 </li>
89 {{ end }}
90 </ul>
91 {{ end }}
92 </li>
93 {{ end }}
94 </ul>
95 {{ end }}
96 ```
97
98 ## Configuration
99
100 See [configure related content][].
101
102 [`keyVals`]: /functions/collections/keyvals/
103 [configure related content]: /configuration/related-content/
104 [this blog article]: https://regisphilibert.com/blog/2018/04/hugo-optmized-relashionships-with-related-content/