github.com/lyeb/hugo@v0.47.1/docs/content/en/templates/taxonomy-templates.md (about)

     1  ---
     2  title: Taxonomy Templates
     3  # linktitle:
     4  description: Taxonomy templating includes taxonomy list pages, taxonomy terms pages, and using taxonomies in your single page templates.
     5  date: 2017-02-01
     6  publishdate: 2017-02-01
     7  lastmod: 2017-02-01
     8  categories: [templates]
     9  keywords: [taxonomies,metadata,front matter,terms,templates]
    10  menu:
    11    docs:
    12      parent: "templates"
    13      weight: 50
    14  weight: 50
    15  sections_weight: 50
    16  draft: false
    17  aliases: [/taxonomies/displaying/,/templates/terms/,/indexes/displaying/,/taxonomies/templates/,/indexes/ordering/, /templates/taxonomies/, /templates/taxonomy/]
    18  toc: true
    19  ---
    20  
    21  <!-- NOTE! Check on https://github.com/gohugoio/hugo/issues/2826 for shifting of terms' pages to .Data.Pages AND
    22  https://discourse.gohugo.io/t/how-to-specify-category-slug/4856/15 for original discussion.-->
    23  
    24  Hugo includes support for user-defined groupings of content called **taxonomies**. Taxonomies are classifications that demonstrate logical relationships between content. See [Taxonomies under Content Management](/content-management/taxonomies) if you are unfamiliar with how Hugo leverages this powerful feature.
    25  
    26  Hugo provides multiple ways to use taxonomies throughout your project templates:
    27  
    28  * Order the way content associated with a taxonomy term is displayed in a [taxonomy list template](#taxonomy-list-template)
    29  * Order the way the terms for a taxonomy are displayed in a [taxonomy terms template](#taxonomy-terms-template)
    30  * List a single content's taxonomy terms within a [single page template][]
    31  
    32  ## Taxonomy List Templates
    33  
    34  Taxonomy list page templates are lists and therefore have all the variables and methods available to [list pages][lists].
    35  
    36  ### Taxonomy List Template Lookup Order
    37  
    38  See [Template Lookup](/templates/lookup-order/).
    39  
    40  ## Taxonomy Terms Template
    41  
    42  ### Taxonomy Terms Templates Lookup Order
    43  
    44  See [Template Lookup](/templates/lookup-order/).
    45  
    46  ### Taxonomy Methods
    47  
    48  A Taxonomy is a `map[string]WeightedPages`.
    49  
    50  .Get(term)
    51  : Returns the WeightedPages for a term.
    52  
    53  .Count(term)
    54  : The number of pieces of content assigned to this term.
    55  
    56  .Alphabetical
    57  : Returns an OrderedTaxonomy (slice) ordered by Term.
    58  
    59  .ByCount
    60  : Returns an OrderedTaxonomy (slice) ordered by number of entries.
    61  
    62  .Reverse
    63  : Returns an OrderedTaxonomy (slice) in reverse order. Must be used with an OrderedTaxonomy.
    64  
    65  ### OrderedTaxonomy
    66  
    67  Since Maps are unordered, an OrderedTaxonomy is a special structure that has a defined order.
    68  
    69  ```
    70  []struct {
    71      Name          string
    72      WeightedPages WeightedPages
    73  }
    74  ```
    75  
    76  Each element of the slice has:
    77  
    78  .Term
    79  : The Term used.
    80  
    81  .WeightedPages
    82  : A slice of Weighted Pages.
    83  
    84  .Count
    85  : The number of pieces of content assigned to this term.
    86  
    87  .Pages
    88  : All Pages assigned to this term. All [list methods][renderlists] are available to this.
    89  
    90  ## WeightedPages
    91  
    92  WeightedPages is simply a slice of WeightedPage.
    93  
    94  ```
    95  type WeightedPages []WeightedPage
    96  ```
    97  
    98  .Count(term)
    99  : The number of pieces of content assigned to this term.
   100  
   101  .Pages
   102  : Returns a slice of pages, which then can be ordered using any of the [list methods][renderlists].
   103  
   104  ## Displaying custom metadata in Taxonomy Terms Templates
   105  
   106  If you need to display custom metadata for each taxonomy term, you will need to create a page for that term at `/content/<TAXONOMY>/<TERM>/_index.md` and add your metadata in it's front matter, [as explained in the taxonomies documentation](/content-management/taxonomies/#add-custom-meta-data-to-a-taxonomy-term). Based on the Actors taxonomy example shown there, within your taxonomy terms template, you may access your custom fields by iterating through the variable `.Pages` as such:
   107  
   108  ```
   109  <ul>
   110    {{ range .Pages }}
   111       <li>
   112         <a href="{{ .Permalink }}">{{ .Title }}</a>
   113         {{ .Params.wikipedia }}
   114       </li>
   115    {{ end }}
   116  </ul>
   117  ```
   118  
   119  <!-- Begin /taxonomies/ordering/ -->
   120  
   121  ## Order Taxonomies
   122  
   123  Taxonomies can be ordered by either alphabetical key or by the number of content pieces assigned to that key.
   124  
   125  ### Order Alphabetically Example
   126  
   127  ```
   128  <ul>
   129    {{ $data := .Data }}
   130    {{ range $key, $value := .Data.Terms.Alphabetical }}
   131    <li><a href="{{ $.Site.LanguagePrefix }}/{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </li>
   132    {{ end }}
   133  </ul>
   134  ```
   135  
   136  ### Order by Popularity Example
   137  
   138  ```
   139  <ul>
   140    {{ $data := .Data }}
   141    {{ range $key, $value := .Data.Terms.ByCount }}
   142    <li><a href="{{ $.Site.LanguagePrefix }}/{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </li>
   143    {{ end }}
   144  </ul>
   145  ```
   146  
   147  ### Order by Least Popular Example
   148  
   149  ```
   150  <ul>
   151    {{ $data := .Data }}
   152    {{ range $key, $value := .Data.Terms.ByCount.Reverse }}
   153    <li><a href="{{ $.Site.LanguagePrefix }}/{{ $data.Plural }}/{{ $value.Name | urlize }}"> {{ $value.Name }} </a> {{ $value.Count }} </li>
   154    {{ end }}
   155  </ul>
   156  ```
   157  
   158  <!-- [See Also Taxonomy Lists](/templates/list/) -->
   159  
   160  ## Order Content within Taxonomies
   161  
   162  Hugo uses both `date` and `weight` to order content within taxonomies.
   163  
   164  Each piece of content in Hugo can optionally be assigned a date. It can also be assigned a weight for each taxonomy it is assigned to.
   165  
   166  When iterating over content within taxonomies, the default sort is the same as that used for [section and list pages]() first by weight then by date. This means that if the weights for two pieces of content are the same, than the more recent content will be displayed first. The default weight for any piece of content is 0.
   167  
   168  ### Assign Weight
   169  
   170  Content can be assigned weight for each taxonomy that it's assigned to.
   171  
   172  ```
   173  +++
   174  tags = [ "a", "b", "c" ]
   175  tags_weight = 22
   176  categories = ["d"]
   177  title = "foo"
   178  categories_weight = 44
   179  +++
   180  Front Matter with weighted tags and categories
   181  ```
   182  
   183  The convention is `taxonomyname_weight`.
   184  
   185  In the above example, this piece of content has a weight of 22 which applies to the sorting when rendering the pages assigned to the "a", "b" and "c" values of the 'tag' taxonomy.
   186  
   187  It has also been assigned the weight of 44 when rendering the 'd' category.
   188  
   189  With this the same piece of content can appear in different positions in different taxonomies.
   190  
   191  Currently taxonomies only support the default ordering of content which is weight -> date.
   192  
   193  <!-- Begin /taxonomies/templates/ -->
   194  
   195  There are two different templates that the use of taxonomies will require you to provide.
   196  
   197  Both templates are covered in detail in the templates section.
   198  
   199  A [list template](/templates/list/) is any template that will be used to render multiple pieces of content in a single html page. This template will be used to generate all the automatically created taxonomy pages.
   200  
   201  A [taxonomy terms template](/templates/terms/) is a template used to
   202  generate the list of terms for a given template.
   203  
   204  <!-- Begin /taxonomies/displaying/ -->
   205  
   206  There are four common ways you can display the data in your
   207  taxonomies in addition to the automatic taxonomy pages created by hugo
   208  using the [list templates](/templates/list/):
   209  
   210  1. For a given piece of content, you can list the terms attached
   211  2. For a given piece of content, you can list other content with the same
   212     term
   213  3. You can list all terms for a taxonomy
   214  4. You can list all taxonomies (with their terms)
   215  
   216  ## Display a Single Piece of Content's Taxonomies
   217  
   218  Within your content templates, you may wish to display the taxonomies that piece of content is assigned to.
   219  
   220  Because we are leveraging the front matter system to define taxonomies for content, the taxonomies assigned to each content piece are located in the usual place (i.e., `.Params.<TAXONOMYPLURAL>`).
   221  
   222  ### Example: List Tags in a Single Page Template
   223  
   224  ```
   225  <ul id="tags">
   226    {{ range .Params.tags }}
   227      <li><a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> </li>
   228    {{ end }}
   229  </ul>
   230  ```
   231  
   232  If you want to list taxonomies inline, you will have to take care of optional plural endings in the title (if multiple taxonomies), as well as commas. Let's say we have a taxonomy "directors" such as `directors: [ "Joel Coen", "Ethan Coen" ]` in the TOML-format front matter.
   233  
   234  To list such taxonomies, use the following:
   235  
   236  ### Example: Comma-delimit Tags in a Single Page Template
   237  
   238  ```
   239  {{ if .Params.directors }}
   240    <strong>Director{{ if gt (len .Params.directors) 1 }}s{{ end }}:</strong>
   241    {{ range $index, $director := .Params.directors }}{{ if gt $index 0 }}, {{ end }}<a href="{{ "directors/" | relURL }}{{ . | urlize }}">{{ . }}</a>{{ end }}
   242  {{ end }}
   243  ```
   244  
   245  Alternatively, you may use the [delimit template function][delimit] as a shortcut if the taxonomies should just be listed with a separator. See {{< gh 2143 >}} on GitHub for discussion.
   246  
   247  ## List Content with the Same Taxonomy Term
   248  
   249  If you are using a taxonomy for something like a series of posts, you can list individual pages associated with the same taxonomy. This is also a quick and dirty method for showing related content:
   250  
   251  ### Example: Showing Content in Same Series
   252  
   253  ```
   254  <ul>
   255    {{ range .Site.Taxonomies.series.golang }}
   256      <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
   257    {{ end }}
   258  </ul>
   259  ```
   260  
   261  ## List All content in a Given taxonomy
   262  
   263  This would be very useful in a sidebar as “featured content”. You could even have different sections of “featured content” by assigning different terms to the content.
   264  
   265  ### Example: Grouping "Featured" Content
   266  
   267  ```
   268  <section id="menu">
   269      <ul>
   270          {{ range $key, $taxonomy := .Site.Taxonomies.featured }}
   271          <li> {{ $key }} </li>
   272          <ul>
   273              {{ range $taxonomy.Pages }}
   274              <li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
   275              {{ end }}
   276          </ul>
   277          {{ end }}
   278      </ul>
   279  </section>
   280  ```
   281  
   282  ## Render a Site's Taxonomies
   283  
   284  If you wish to display the list of all keys for your site's taxonomy, you can retrieve them from the [`.Site` variable][sitevars] available on every page.
   285  
   286  This may take the form of a tag cloud, a menu, or simply a list.
   287  
   288  The following example displays all terms in a site's tags taxonomy:
   289  
   290  ### Example: List All Site Tags
   291  
   292  ```
   293  <ul id="all-tags">
   294    {{ range $name, $taxonomy := .Site.Taxonomies.tags }}
   295      <li><a href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}">{{ $name }}</a></li>
   296    {{ end }}
   297  </ul>
   298  ```
   299  
   300  ### Example: List All Taxonomies, Terms, and Assigned Content
   301  
   302  This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.
   303  
   304  {{< code file="layouts/partials/all-taxonomies.html" download="all-taxonomies.html" download="all-taxonomies.html" >}}
   305  <section>
   306    <ul id="all-taxonomies">
   307      {{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
   308        <li><a href="{{ "/" | relLangURL}}{{ $taxonomyname | urlize }}">{{ $taxonomyname }}</a>
   309          <ul>
   310            {{ range $key, $value := $taxonomy }}
   311            <li> {{ $key }} </li>
   312                  <ul>
   313                  {{ range $value.Pages }}
   314                      <li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
   315                  {{ end }}
   316                  </ul>
   317            {{ end }}
   318          </ul>
   319        </li>
   320      {{ end }}
   321    </ul>
   322  </section>
   323  {{< /code >}}
   324  
   325  ## `.Site.GetPage` for Taxonomies
   326  
   327  Because taxonomies are lists, the [`.GetPage` function][getpage] can be used to get all the pages associated with a particular taxonomy term using a terse syntax. The following ranges over the full list of tags on your site and links to each of the individual taxonomy pages for each term without having to use the more fragile URL construction of the "List All Site Tags" example above:
   328  
   329  {{< code file="links-to-all-tags" >}}
   330  <ul class="tags">
   331    {{ range ($.Site.GetPage "taxonomyTerm" "tags").Pages }}
   332     <li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
   333    {{ end }}
   334  </ul>
   335  {{< /code >}}
   336  
   337  <!--### `.Site.GetPage` Taxonomy List Example
   338  
   339  ### `.Site.GetPage` Taxonomy Terms Example -->
   340  
   341  
   342  [delimit]: /functions/delimit/
   343  [getpage]: /functions/getpage/
   344  [lists]: /templates/lists/
   345  [renderlists]: /templates/lists/
   346  [single page template]: /templates/single-page-templates/
   347  [sitevars]: /variables/site/