GetPage.md (3087B)
1 --- 2 title: .GetPage 3 description: "Gets a `Page` of a given `path`." 4 date: 2017-02-01 5 publishdate: 2017-02-01 6 lastmod: 2017-02-01 7 categories: [functions] 8 menu: 9 docs: 10 parent: "functions" 11 keywords: [sections,lists,indexes] 12 signature: [".GetPage PATH"] 13 workson: [] 14 hugoversion: 15 relatedfuncs: [] 16 deprecated: false 17 aliases: [] 18 --- 19 20 `.GetPage` returns a page of a given `path`. Both `Site` and `Page` implements this method. The `Page` variant will, if given a relative path -- i.e. a path without a leading `/` -- try look for the page relative to the current page. 21 22 {{% note %}} 23 **Note:** We overhauled and simplified the `.GetPage` API in Hugo 0.45. Before that you needed to provide a `Kind` attribute in addition to the path, e.g. `{{ .Site.GetPage "section" "blog" }}`. This will still work, but is now superfluous. 24 {{% /note %}} 25 26 27 ```go-html-template 28 {{ with .Site.GetPage "/blog" }}{{ .Title }}{{ end }} 29 ``` 30 31 This method will return `nil` when no page could be found, so the above will not print anything if the blog section is not found. 32 33 To find a regular page in the blog section:: 34 35 ```go-html-template 36 {{ with .Site.GetPage "/blog/my-post.md" }}{{ .Title }}{{ end }} 37 ``` 38 39 And since `Page` also provides a `.GetPage` method, the above is the same as: 40 41 ```go-html-template 42 {{ with .Site.GetPage "/blog" }} 43 {{ with .GetPage "my-post.md" }}{{ .Title }}{{ end }} 44 {{ end }} 45 ``` 46 47 ## .GetPage and Multilingual Sites 48 49 The previous examples have used the full content filename to lookup the post. Depending on how you have organized your content (whether you have the language code in the file name or not, e.g. `my-post.en.md`), you may want to do the lookup without extension. This will get you the current language's version of the page: 50 51 ```go-html-template 52 {{ with .Site.GetPage "/blog/my-post" }}{{ .Title }}{{ end }} 53 ``` 54 55 ## .GetPage Example 56 57 This code snippet---in the form of a [partial template][partials]---allows you to do the following: 58 59 1. Grab the index object of your `tags` [taxonomy][]. 60 2. Assign this object to a variable, `$t` 61 3. Sort the terms associated with the taxonomy by popularity. 62 4. Grab the top two most popular terms in the taxonomy (i.e., the two most popular tags assigned to content. 63 64 {{< code file="grab-top-two-tags.html" >}} 65 <ul class="most-popular-tags"> 66 {{ $t := .Site.GetPage "/tags" }} 67 {{ range first 2 $t.Data.Terms.ByCount }} 68 <li>{{ . }}</li> 69 {{ end }} 70 </ul> 71 {{< /code >}} 72 73 ## `.GetPage` on Page Bundles 74 75 If the page retrieved by `.GetPage` is a [Leaf Bundle][leaf_bundle], and you 76 need to get the nested _**page** resources_ in that, you will need to use the 77 methods in `.Resources` as explained in the [Page Resources][page_resources] 78 section. 79 80 See the [Headless Bundle][headless_bundle] documentation for an example. 81 82 83 [partials]: /templates/partials/ 84 [taxonomy]: /content-management/taxonomies/ 85 [page_kinds]: /templates/section-templates/#page-kinds 86 [leaf_bundle]: /content-management/page-bundles/#leaf-bundles 87 [headless_bundle]: /content-management/page-bundles/#headless-bundle 88 [page_resources]: /content-management/page-resources/