HUGO

How to List tags specify a section with use insert every certain amount

first.I use this code to list content and it can insert html(banner) every 3\7 amount

{{- range $article_index, $article_array := .Data.Pages -}}
	{{- if eq $article_index 3 -}}<div id="banner1"></div>{{- end -}}
	{{- if eq $article_index 8 -}}<div id="banner2"></div>{{- end -}}
	<li><a href="{{- .Permalink -}}">{{- .Title -}}</a></li>
{{- end -}}

but when I want to list tags specify a section:

{{- range $article_index, $article_array := where .Site.Taxonomies.tags ".Page.Section" "posts" -}}
	{{- if eq $article_index 3 -}}<div id="banner1"></div>{{- end -}}
	{{- if eq $article_index 8 -}}<div id="banner2"></div>{{- end -}}
	<li><a href="{{- .Permalink -}}">{{- .Title -}}</a></li>
{{- end -}}

It can’t insert html(banner) but no error
How to do if want the above two feature
Thanks.

add: template path /layout/tags/terms.html

This does not answer your question but you should know your html is invalid.

  1. The only permitted content of a list element is <li>, <script> or <template>
  2. ids must be unique. Your code will result in multiple <div id="banner">

oh sorry. I go to fix that

I don’t solve that program yet. can anybody help? thanks.

The data structure for .Site.Taxonomies.tags looks something like this:

{
  "tag-a": [
    {
      "Weight": 0,
      "Page": {
        "Content": "\u003cp\u003eThis is test one.\u003c/p\u003e\n",
        "Section": "post",
        "Title": "Test 1",
        "Weight": 10,
        ...
      }
    },
    {
      "Weight": 0,
      "Page": {
        "Content": "\u003cp\u003eThis is test two.\u003c/p\u003e\n",
        "Section": "post",
        "Title": "Test 2",
        "Weight": 20,
        ...
      }
    }
  ],
  "tag-b": [
    {
      "Weight": 0,
      "Page": {
        "Content": "\u003cp\u003eThis is test three.\u003c/p\u003e\n",
        "Section": "post",
        "Title": "Test 3",
        "Weight": 30,
        ...
      }
    }
  ]
}

Note that the top level keys are strings (the term names) not integers.

One would normally range through this collection doing something like:

{{ range $term, $weightedPages := .Site.Taxonomies.tags }}
  <h2>{{ $term }}</h2>
  {{ range $weightedPages }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a><br>
  {{ end }}
{{ end }}

thanks for reply
yes. I googled it show me that. but when I only need to show a specific section’s tags I don’t know how to do.
I only know use .Site.Taxonomies.tags ".Page.Section" "posts" to show a specific section‘s tags. but it can’t let if eq $article_index 3 take effect
Is there a way to do both?

Use a counter.

{{ $i := 0}}
{{ range $term, $weightedPages := where .Site.Taxonomies.tags "Page.Section" "posts" }}
  {{ $termPage := site.GetPage (path.Join "tags" $term) }}
  {{ if eq $i 3 }}
    {{/* YOUR BANNER HERE */}}
  {{ end }}
  <h2><a href="{{ $termPage.RelPermalink }}">{{ $termPage.Title }}</a></h2>
  {{ $i = add 1 $i }}
{{ end }}

thanks. It’s worked!
I’m a website operator, and I don’t know the actual working logic of the code. Therefore, in the face of some situations like this, the official documents cannot completely solve the doubts. Thanks community. great!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.