Author page is displayed twice

Hello I have a problem the
for the same author (duplicate authors) at the top of each book, in the author page! The image below explains the problem: Here is my code (taxonomy.html) responsible for the authors page:

<div class="content-box">
{{ range .Paginator.Pages }}    

<div class="list-content" style="background:none !important;">

{{ range .Param "authors" }}
	{{ $name := . }}
	{{ $path := printf "/%s/%s" "authors" ($name | urlize) }}
	{{ with $.Site.GetPage $path }}
		<h2>{{ .Params.name }}</h2>
		<div class="ImgAut">
		<img src="/images{{ $path }}.png">
		</div>
		<p>{{ .Params.bio }}</p>
	{{ end }}
{{ end }}
.....

This is what chatgpt told me try it out
Your code is currently looping through each page in the paginator and then, for each page, looping through the list of authors. This is causing the duplicate author entries for pages that share the same author.

To fix this, you should first gather a unique list of authors and then loop through that list. Here’s a solution to avoid duplicate authors:

  1. First, create a set of unique authors.
  2. Next, loop through the unique set to display author details.

Here’s how you can do it:

<div class="content-box">
{{ $uniqueAuthors := slice }}
{{ range .Paginator.Pages }}
  {{ range .Param "authors" }}
    {{ $name := . }}
    {{ if not (in $uniqueAuthors $name) }}
      {{ $uniqueAuthors = $uniqueAuthors | append $name }}
    {{ end }}
  {{ end }}
{{ end }}

{{ range $uniqueAuthors }}
  {{ $name := . }}
  {{ $path := printf "/%s/%s" "authors" ($name | urlize) }}
  {{ with $.Site.GetPage $path }}
    <div class="list-content" style="background:none !important;">
      <h2>{{ .Params.name }}</h2>
      <div class="ImgAut">
        <img src="/images{{ $path }}.png">
      </div>
      <p>{{ .Params.bio }}</p>
    </div>
  {{ end }}
{{ end }}
</div>

Here’s the breakdown of the changes:

  1. Initialize an empty slice $uniqueAuthors to store unique author names.
  2. Loop through all the pages and their authors, and only append the author to the $uniqueAuthors slice if they haven’t been added already.
  3. Once you have the unique authors, loop through them to display their details.

This approach ensures that each author is displayed only once, regardless of how many times they appear in the list of authors across different pages.

1 Like

Do you really need the .Paginator?

yes i use paginator

Okay. I thought you were just showing a list of authors. But if it is a list of authors and their pages, that’s understandable.

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