Related posts in a different way

I do not like build in related posts feature.
How to modify $related variable value,
so hugo takes 1st tag of the post, then display newest 3rd, 4th, 5th, 6th, 7th post in that tag taxonomy.

<section>
      {{ $related := .Site.RegularPages.Related . | first 5 }}
      {{ if (.Site.RegularPages.Related . ) }}
      <div class="tag-posts-section">
        <div class="section-title">
        </div>
        <div class="latest-tag-posts">
          {{ with $related }}
          {{- partial "related.html" . }}
          {{ end }}
        </div>
      </div>
      {{ end }}
    </section>

configuration

[related]
includeNewer = true
threshold = 80
toLower = false

[[related.indices]]
name = 'tags'
weight = 80
layouts/_default/single.html
{{- $taxonomy := "tags" }}
{{- $firstTerm := index (index .Params $taxonomy) 0 }}
{{- $pages := where site.RegularPages "Type" "posts" }}
{{- $related := $pages.RelatedTo (keyVals $taxonomy $firstTerm) }}

{{- with $related }}
  {{- with .ByDate.Reverse | first 5 }}
    <div class="related-pages">
      <div class="related-pages-title">Related pages</div>
      <ul class="related-pages-list">
        {{- range . }}
          <li class="related-pages-list-item">
            <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
          </li>
        {{- end }}
      </ul>
    </div>
  {{- end }}
{{- end }}

1 Like

Thank you. How to add “after 3” to this code, so it is first 5 posts after third?
This will work great on the homepage, but on post page I would like to display first 5 after 3rd, but older than “that post”.
P.S. includeNewer = false I will use.

{{- with .ByDate.Reverse | first 5 }}

Change this:

{{- with .ByDate.Reverse | first 5 }}

To this:

{{- with .ByDate.Reverse | after 3 | first 5 }}

Excellent. Now my homepage displays related posts after 3, first 5 posts from “posts” section.

Posts have the same section and would like to modify it so after 3, first 5 related posts will be displayed, counting those with .Params.date older than current viewed post.

I don’t understand. Are you confirming that is working, or do you need additional assistance?

I use this code on the homepage as well as in a post.
Now this code works well on homepage, but I want to modify it in post template.

I want to achieve in a post: after 3, first 5 related posts will be displayed, counting those from .Params.date older than current viewed post. Then each post will display different related posts - always older 5 posts after 3.

Because you are using this in more than one template, it makes sense to use a parameterized partial.

layouts/partials/related.html
{{- /*
Renders a list of pages related by the first term on the current page in the given taxonomy.

The following parameters are optional: excludeNewer, after, first, and debug.

@param {page} currentPage The current page.
@param {page.Pages} pageCollection The collection of pages from which to select the related pages.
@param {string} taxonomy The taxonomy used to determine related pages.
@param {bool} [excludeNewer=false] If true, exclude pages newer than the current page.
@param {int} [after=0] Starting with the collection of related pages (sorted descending by date) skip the first N.
@param {int} [first=1000] After skipping the first N pages, include N more.
@param {bool} [debug=false] If true, displays date and tags next to each list item.

@returns {template.HTML}

@example

  {{ $ctx := dict
    "currentPage" .
    "pageCollection" (where site.RegularPages "Type" "posts")
    "taxonomy" "tags"
    "excludeNewer" true
    "after" 3
    "first" 5
    "debug" true
  }}
  {{ partial "related.html" $ctx }}

  OR

  {{ $ctx := dict
    "currentPage" .
    "pageCollection" (where site.RegularPages "Type" "posts")
    "taxonomy" "tags"
  }}
  {{ partial "related.html" $ctx }}

*/}}

{{- /* Get context. */}}
{{- $currentPage := .currentPage }}
{{- $pageCollection := .pageCollection }}
{{- $taxonomy := .taxonomy }}
{{- $excludeNewer := or .excludeNewer false }}
{{- $after := or (int .after) 0 }}
{{- $first := or (int .first) 1000 }}
{{- $debug := or .debug false }}

{{- /* Initialize. */}}
{{- $partialName := "related.html" }}
{{- $error := false }}

{{- /* Validate. */}}
{{- if not (and $currentPage $pageCollection $taxonomy) }}
  {{- $error = true }}
  {{- errorf "The %q partial requires the following context: page, pageCollection, and taxonomy" $partialName }}
{{- end }}
{{- if not (index site.Taxonomies $taxonomy) }}
  {{- $error = true }}
  {{- errorf "The %q partial received an invalid taxonomy %q in context" $partialName $taxonomy }}
{{- end }}

{{- if not $error }}

  {{- /* Remove current page from page collection. */}}
  {{- $pages := $pageCollection | complement (slice $currentPage) }}

  {{- /* Limit page collection to pages with .Date earlier than current page .Date. */}}
  {{- if $excludeNewer }}
    {{- $pages = where $pages "Date" "lt" $currentPage.Date }}
  {{- end }}

  {{- /* Within the page collection, get pages related by the first term on the current page in the given taxonomy. */}}
  {{- $firstTerm := index (index $currentPage.Params $taxonomy) 0 }}
  {{- $related := $pages.RelatedTo (keyVals $taxonomy $firstTerm) }}

  {{/* Limit related pages by after and first parameters. */}}
  {{- $related = $related.ByDate.Reverse | after $after | first $first }}

  {{- with $related }}
    <div class="related-pages">
      <div class="related-pages-title">Related pages:</div>
      <ul class="related-pages-list">
        {{- range . }}
          <li class="related-pages-list-item">
            <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>

            {{- if $debug }}
              <div style="display: inline-block; background: #FAFAD2">
                {{- $tv := .Date | time.Format "2006-01-02T15:04:05-07:00" }}
                {{- $td := .Date | time.Format "2006-01-02" -}}
                <time style="margin: 0 .5em;" {{ printf "datetime=%q" $tv | safeHTMLAttr }}>{{ $td }}</time>
                {{- with .GetTerms "tags" }}
                  {{- range . }}
                    <a style="margin: 0 .5em;" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
                  {{- end }}
                {{- end }}
              </div>
            {{- end }}

          </li>
        {{- end }}
      </ul>
    </div>
  {{- end }}

{{- end }}


Then call it from your home page template with:

{{ $ctx := dict
  "currentPage" .
  "pageCollection" (where site.RegularPages "Type" "posts")
  "taxonomy" "tags"
  "after" 3
  "first" 5
}}
{{ partial "related.html" $ctx }}

And call it from your single page template with:

{{ $ctx := dict
  "currentPage" .
  "pageCollection" (where site.RegularPages "Type" "posts")
  "taxonomy" "tags"
  "excludeNewer" true
  "after" 3
  "first" 5
}}
{{ partial "related.html" $ctx }}
1 Like