hugo/docs/content/en/methods/page/Type.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

1.5 KiB

title description categories keywords action
Type Returns the content type of the given page.
related returnType signatures
methods/page/Kind
methods/page/Layout
methods/page/Type
string
PAGE.Type

The Type method on a Page object returns the content type of the given page. The content type is defined by the type field in front matter, or inferred from the top-level directory name if the type field in front matter is not defined.

With this content structure:

content/
├── auction/
│   ├── _index.md
│   ├── item-1.md
│   └── item-2.md  <-- front matter: type = books
├── books/
│   ├── _index.md
│   ├── book-1.md
│   └── book-2.md
├── films/
│   ├── _index.md
│   ├── film-1.md 
│   └── film-2.md
└── _index.md

To list the books, regardless of section:

{{ range where .Site.RegularPages.ByTitle "Type" "books" }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}

Hugo renders this to;

<h2><a href="/books/book-1/">Book 1</a></h2>
<h2><a href="/books/book-2/">Book 2</a></h2>
<h2><a href="/auction/item-2/">Item 2</a></h2>

The type field in front matter is also useful for targeting a template. See details.