Debian Codegraph @

docs/content/en/methods/site/GetPage.md

raw
1 ---
2 title: GetPage
3 description: Returns a Page object from the given path.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: page.Page
9 signatures: [SITE.GetPage PATH]
10 ---
11
12 The `GetPage` method is also available on `Page` objects, allowing you to specify a path relative to the current page. See [details].
13
14 [details]: /methods/page/getpage/
15
16 When using the `GetPage` method on a `Site` object, specify a path relative to the `content` directory.
17
18 If Hugo cannot resolve the path to a page, the method returns nil.
19
20 Consider this content structure:
21
22 ```text
23 content/
24 ├── works/
25 │ ├── paintings/
26 │ │ ├── _index.md
27 │ │ ├── starry-night.md
28 │ │ └── the-mona-lisa.md
29 │ ├── sculptures/
30 │ │ ├── _index.md
31 │ │ ├── david.md
32 │ │ └── the-thinker.md
33 │ └── _index.md
34 └── _index.md
35 ```
36
37 This _home_ template:
38
39 ```go-html-template {file="layouts/home.html"}
40 {{ with .Site.GetPage "/works/paintings" }}
41 <ul>
42 {{ range .Pages }}
43 <li>{{ .Title }} by {{ .Params.artist }}</li>
44 {{ end }}
45 </ul>
46 {{ end }}
47 ```
48
49 Is rendered to:
50
51 ```html
52 <ul>
53 <li>Starry Night by Vincent van Gogh</li>
54 <li>The Mona Lisa by Leonardo da Vinci</li>
55 </ul>
56 ```
57
58 To get a regular page instead of a section page:
59
60 ```go-html-template {file="layouts/home.html"}
61 {{ with .Site.GetPage "/works/paintings/starry-night" }}
62 {{ .Title }} → Starry Night
63 {{ .Params.artist }} → Vincent van Gogh
64 {{ end }}
65 ```
66
67 ## Multilingual projects
68
69 With multilingual projects, the `GetPage` method on a `Site` object resolves the given path to a page in the current language.
70
71 To get a page from a different language, query the `Sites` object:
72
73 ```go-html-template
74 {{ with where hugo.Sites "Language.Name" "eq" "de" }}
75 {{ with index . 0 }}
76 {{ with .GetPage "/works/paintings/starry-night" }}
77 {{ .Title }} → Sternenklare Nacht
78 {{ end }}
79 {{ end }}
80 {{ end }}
81 ```
82
83 ## Page bundles
84
85 Consider this content structure:
86
87 ```text
88 content/
89 ├── headless/
90 │ ├── a.jpg
91 │ ├── b.jpg
92 │ ├── c.jpg
93 │ └── index.md <-- front matter: headless = true
94 └── _index.md
95 ```
96
97 In the _home_ template, use the `GetPage` method on a `Site` object to render all the images in the headless [page bundle](g):
98
99 ```go-html-template {file="layouts/home.html"}
100 {{ with .Site.GetPage "/headless" }}
101 {{ range .Resources.ByType "image" }}
102 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
103 {{ end }}
104 {{ end }}
105 ```