Can’t evaluate field ByCount in type page.Page

I’m trying to sort my taxonomy page for tags by how many pages each tag has.
To do that, I’m using {{ range .ByCount }} (I also tried .Pages.ByCount). However, even though the docs and everything I’ve read online seems to indicate that this should work, I’m getting the error:

ERROR render of "term" failed: "…/blog/layouts/_default/list.html:3:9": execute of template failed: template: _default/list.html:3:9: executing "main" at <.ByCount>: can't evaluate field ByCount in type *hugolib.pageState
ERROR render of "section" failed: "…/blog/layouts/_default/list.html:3:9": execute of template failed: template: _default/list.html:3:9: executing "main" at <.ByCount>: can't evaluate field ByCount in type *hugolib.pageState
ERROR Rebuild failed: render: failed to render pages: render of "taxonomy" failed: "…/blog/layouts/_default/list.html:3:9": execute of template failed at <.ByCount>: can’t evaluate field ByCount in type page.Page

(edited to remove identifying information from the path).

The code for the template in question is as follows:

{{ define "main" }}
<h1>{{ if not (eq (i18n .Title) "") }}{{ i18n .Title }}{{ else }}{{ .Title }}{{ end }}</h1>
{{ range .ByCount }}
<div class="entry">
	<a class="coverlink" href="{{ .RelPermalink }}"><span class="linkcover"></span></a>
	<span class="inline-flex space-between mobile-flex-column">
		<a class="post-title" href="{{ .RelPermalink }}">
			{{ .Title }}
		</a>
		<span class="horizontal-spacing"></span>
		{{ partial "metadata.html" . }}
	</span>
	{{ if (isset .Params "description") }}
		<div class="entry-description">
			{{ .Params.Description }}
		</div>
	{{ else }}
		{{ with .Summary }}
			<div class="entry-summary">
				{{ . }}
			</div>
		{{ end }}
	{{ end }}
</div>
{{ end }}
{{ end }}

Any help would be greatly appreciated, I have no idea why this isn’t working.

As simple search for “bycount” in the forum turns up this post:

which seems to deal with your problem, too.

You need to use different templates for section, taxonomy, and term pages.

layouts/
└── _default/
    ├── baseof.html
    ├── home.html
    ├── list.html
    ├── single.html
    ├── taxonomy.html
    └── term.html

layouts/_default/taxonomy.html

{{ define "main" }} 
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Data.Terms.ByCount }}
    <p><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> (count = {{ .Count }})</p>
      <ul>
      {{ range .Pages }}
        <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
      {{ end }}
    </ul>
  {{ end }}
{{ end }}

rendered

image

Thanks! That worked.

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