# セクション内で使われているカテゴリのリストを表示したい

**URL:** https://discourse.gohugo.io/t/topic/23092
**Category:** 日本語
**Created:** [January 28, 2020, 5:01pm UTC](https://discourse.gohugo.io/t/topic/23092 "2020-01-28T17:01:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![anocco](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/anocco/32/10510_2.png) [@anocco](https://discourse.gohugo.io/u/anocco)
#### Post date: [January 28, 2020, 5:01pm UTC](https://discourse.gohugo.io/t/topic/23092/1 "2020-01-28T17:01:51Z")

</div>

[teratail](https://teratail.com/questions/237881)でも質問しましたが、こちらでも質問させていただきます。

### 前提・実現したいこと

**セクション内にある記事で使われているカテゴリのリストを表示したい**  
(以下をご参照ください)

### 現状・問題点

静的サイトジェネレーターHUGOを触り始めた初心者です。  
セクション内にある記事で使われているカテゴリだけのリストを表示する方法も探しています。  
現状は、【All・カテゴリA・カテゴリB・カテゴリC・カテゴリD】になっていますが、  
これを  
section1のページ…【All・カテゴリA・カテゴリB】  
section2のページ…【All・カテゴリC・カテゴリD】  
にしたいということです。  
※Allはsection○のトップページに飛ぶようにしています。

### 発生している問題・エラーメッセージ

実験中のため`index.md`だったり、`_index.md`だったりバラバラです。

```auto
サイト名/
        ├ archetypes/
        ├ content/
        │ ├ section1/
        │ │ ├ categoryA/
        │ │ │ ├ post
        │ │ │ │ └ index.md
        │ │ │ │
        │ │ │ └ _index.md
        │ │ │
        │ │ ├ categoryB/
        │ │ │ ├ _index.md
        │ │ │ └ post2.md
        │ │ │
        │ │ ├ _index.md
        │ │ └ post3.md
        │ │
        │ ├ section2/
        │ │ ├ categoryC/
        │ │ │ ├ index.md
        │ │ │ └ post4.md
        │ │ │
        │ │ ├ categoryD/
        │ │ │ ├ _index.md
        │ │ │ └ post5.md
        │ │ │
        │ │ ├ _index.md
        │ │ └ post6.md
        ├ layouts/
        │ └ _default
        │ ├ list.html
        │ └ post.html

```

### 該当のソースコード

```auto
<!-- layouts/_default/list.html -->

{{ define "main" }}
  {{ partial "page-title.html" . }}
  <!-- blog(section1) -->
  <section class="section">
    <div class="container">
       <ul>
		  {{ with .CurrentSection }}
		    <li><a href="{{ .RelPermalink }}">All</a></li>
		  {{- end }}
		  {{- range $taxonomyName, $taxonomy := .Site.Taxonomies }}
		   {{- range ($.Site.GetPage "taxonomyTerm" $taxonomyName).Pages }}
           <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
           {{- end }}
		  {{- end }}
      </ul>
      <div class="row">
        {{ range .Paginator.Pages }}
        {{ .Render "post" }}
        {{ end }}
      </div>
	{{ template "_internal/pagination.html" . }}
    </div>
  </section>
  <!-- /blog(section1) -->
{{ end }}

<!-- layouts/_default/post.html -->

<div class="col-lg-4 col-sm-6 mb-4">
  <article class="card shadow">
    <img class="rounded card-img-top" src="{{.Params.Image | relURL}}" alt="{{.Title }}">
    <div class="card-body">
      <h4 class="card-title"><a class="text-dark" href="{{.RelPermalink}}">{{.Title }}</a>
      </h4>
      <p class="cars-text">{{.Summary}}…</p>
      <a href="{{.RelPermalink}}" class="btn btn-xs btn-primary">Read More</a>
    </div>
  </article>
</div>

```

### 試したこと

```auto
<!-- layouts/_default/list.html -->

 {{- range $taxonomyName, $taxonomy := .Site.Taxonomies }}
		   {{- range ($.Site.GetPage "taxonomyTerm" $taxonomyName).Pages }}
           <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
           {{- end }}
		  {{- end }}

```

この部分が怪しいと思っています。  
`.Site.Taxonomies`はサイト全体で使用しているタクソノミーを取得しているという認識で合っていますか？  
`.Site.GetPage`でセクション内の記事を取得出来るということは分かったのですが、セクション内のタクソノミーだけをを取得する方法が分かりません。

### 補足情報（FW/ツールのバージョンなど）

こちらのテンプレートを使用しています。  
[Kross hugo](https://themes.gohugo.io//theme/kross-hugo-portfolio-template/)  
このデモサイトのポートフォリオページのカテゴリタブのようなものを、ブログのほうでも実現したいということです。

---

<div class="post-metadata">

### Author: ![peaceiris](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/peaceiris/32/7358_2.png) [@peaceiris](https://discourse.gohugo.io/u/peaceiris)
#### Post date: [February 3, 2020, 2:10am UTC](https://discourse.gohugo.io/t/topic/23092/2 "2020-02-03T02:10:41Z")

</div>

> [@anocco](#):
>
> `.Site.Taxonomies` はサイト全体で使用しているタクソノミーを取得しているという認識で合っていますか？

合っています。

要件を聞く限りだと「`section1` 内のセクション一覧を表示する」でも同じではないかと思います。

`Taxonomies` を使わずに categoryA とかもセクションとして扱う方が簡単です。(実際、Hugo は `_index.md` が配置されたディレクトリをセクションとして認識します。) Taxonomies を利用するには記事のフロントマターに `category: categoryA` を記述する必要もありますし、二度手間だと思うので。

---

<div class="post-metadata">

### Author: ![anocco](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/anocco/32/10510_2.png) [@anocco](https://discourse.gohugo.io/u/anocco)
#### Post date: [February 5, 2020, 5:03am UTC](https://discourse.gohugo.io/t/topic/23092/3 "2020-02-05T05:03:52Z")

</div>

ご回答ありがとうございます。  
なるほど…。応急処置で

```
{{ if eq .Section "section1" }}
{{ if eq .Title "Categories" "categoryA" }}

```

などの分岐を使って表示する内容を変えていましたが、カテゴリもセクションとして扱ったほうが確かに簡単ですね。こちらの内容で試してみます。
