| 1 |
--- |
| 2 |
title: Related |
| 3 |
description: Returns a collection of pages related to the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: page.Pages |
| 9 |
signatures: |
| 10 |
- PAGES.Related PAGE |
| 11 |
- PAGES.Related OPTIONS |
| 12 |
--- |
| 13 |
|
| 14 |
Based on front matter, Hugo uses several factors to identify content related to the given page. Use the default [related content configuration], or tune the results to the desired indices and parameters. See [details]. |
| 15 |
|
| 16 |
The argument passed to the `Related` method may be a `Page` or an options map. For example, to pass the current page: |
| 17 |
|
| 18 |
```go-html-template {file="layouts/page.html"} |
| 19 |
{{ with .Site.RegularPages.Related . | first 5 }} |
| 20 |
<p>Related pages:</p> |
| 21 |
<ul> |
| 22 |
{{ range . }} |
| 23 |
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li> |
| 24 |
{{ end }} |
| 25 |
</ul> |
| 26 |
{{ end }} |
| 27 |
``` |
| 28 |
|
| 29 |
To pass an options map: |
| 30 |
|
| 31 |
```go-html-template {file="layouts/page.html"} |
| 32 |
{{ $opts := dict |
| 33 |
"document" . |
| 34 |
"indices" (slice "tags" "keywords") |
| 35 |
}} |
| 36 |
{{ with .Site.RegularPages.Related $opts | first 5 }} |
| 37 |
<p>Related pages:</p> |
| 38 |
<ul> |
| 39 |
{{ range . }} |
| 40 |
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li> |
| 41 |
{{ end }} |
| 42 |
</ul> |
| 43 |
{{ end }} |
| 44 |
``` |
| 45 |
|
| 46 |
## Options |
| 47 |
|
| 48 |
indices |
| 49 |
: (`slice`) The indices to search within. |
| 50 |
|
| 51 |
document |
| 52 |
: (`page`) The page for which to find related content. Required when specifying an options map. |
| 53 |
|
| 54 |
namedSlices |
| 55 |
: (`slice`) The keywords to search for, expressed as a slice of `KeyValues` using the [`keyVals`] function. |
| 56 |
|
| 57 |
[`keyVals`]: /functions/collections/keyvals/ |
| 58 |
|
| 59 |
fragments |
| 60 |
: (`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. |
| 61 |
|
| 62 |
A contrived example using all of the above: |
| 63 |
|
| 64 |
```go-html-template |
| 65 |
{{ $page := . }} |
| 66 |
{{ $opts := dict |
| 67 |
"indices" (slice "tags" "keywords") |
| 68 |
"document" $page |
| 69 |
"namedSlices" (slice (keyVals "tags" "hugo" "rocks") (keyVals "date" $page.Date)) |
| 70 |
"fragments" (slice "heading-1" "heading-2") |
| 71 |
}} |
| 72 |
``` |
| 73 |
|
| 74 |
[details]: /content-management/related-content/ |
| 75 |
[related content configuration]: /configuration/related-content/ |