github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/docs/content/en/functions/GetPage.md (about) 1 --- 2 title: .GetPage 3 description: "Gets a `Page` of a given `Kind` and `path`." 4 godocref: 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [functions] 9 menu: 10 docs: 11 parent: "functions" 12 keywords: [sections,lists,indexes] 13 signature: [".GetPage KIND PATH"] 14 workson: [] 15 hugoversion: 16 relatedfuncs: [] 17 deprecated: false 18 aliases: [] 19 --- 20 21 Every `Page` has a [`Kind` attribute][page_kinds] that shows what kind of page it is. While this attribute can be used to list pages of a certain `kind` using `where`, often it can be useful to fetch a single page by its path. 22 23 `.GetPage` returns a page of a given `Kind` and `path`. 24 25 {{% note %}} 26 If the `path` is `"foo/bar.md"`, it can be written as exactly that, or broken up 27 into multiple strings as `"foo" "bar.md"`. 28 {{% /note %}} 29 30 ``` 31 {{ with .Site.GetPage "section" "blog" }}{{ .Title }}{{ end }} 32 ``` 33 34 This method wil return `nil` when no page could be found, so the above will not print anything if the blog section is not found. 35 36 For a regular page (whose `Kind` is `page`): 37 38 ``` 39 {{ with .Site.GetPage "page" "blog/my-post.md" }}{{ .Title }}{{ end }} 40 ``` 41 42 Note that the `path` can also be supplied like this, where the slash-separated 43 path elements are added as separate strings: 44 45 ``` 46 {{ with .Site.GetPage "page" "blog" "my-post.md" }}{{ .Title }}{{ end }} 47 ``` 48 49 ## `.GetPage` Example 50 51 This code snippet---in the form of a [partial template][partials]---allows you to do the following: 52 53 1. Grab the index object of your `tags` [taxonomy][]. 54 2. Assign this object to a variable, `$t` 55 3. Sort the terms associated with the taxonomy by popularity. 56 4. Grab the top two most popular terms in the taxonomy (i.e., the two most popular tags assigned to content. 57 58 {{< code file="grab-top-two-tags.html" >}} 59 <ul class="most-popular-tags"> 60 {{ $t := .Site.GetPage "taxonomyTerm" "tags" }} 61 {{ range first 2 $t.Data.Terms.ByCount }} 62 <li>{{ . }}</li> 63 {{ end }} 64 </ul> 65 {{< /code >}} 66 67 ## `.GetPage` on Page Bundles 68 69 If the page retrieved by `.GetPage` is a [Leaf Bundle][leaf_bundle], and you 70 need to get the nested _**page** resources_ in that, you will need to use the 71 methods in `.Resources` as explained in the [Page Resources][page_resources] 72 section. 73 74 See the [Headless Bundle][headless_bundle] documentation for an example. 75 76 77 [partials]: /templates/partials/ 78 [taxonomy]: /content-management/taxonomies/ 79 [page_kinds]: /templates/section-templates/#page-kinds 80 [leaf_bundle]: /content-management/page-bundles/#leaf-bundles 81 [headless_bundle]: /content-management/page-bundles/#headless-bundle 82 [page_resources]: /content-management/page-resources/