Add related.html to layouts/partials

related.html

<!-- Find all other articles with the same tags as the current article, excluding the current article itself, and then take the first 5 such related articles and assign them to the variable $related -->
{{ $related := first 5 (where (where .Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink)}}
<!-- Check to see if a related article exists. If it does, then execute the following code -->
{{ with $related }}
<h3 class="see-also">{{- i18n "related" -}}</h3>
<div class="related">
  <ul>
    {{ range . }}
    <li>
      <a href="{{ .RelPermalink }}" target="_blank">{{ .Title }}</a> <span class="related-date">({{ .Date | time.Format
        "2006-01-02" }})</span>
    </li>
    {{ end }}
  </ul>
</div>
{{ end }}

2.Add Style

Add related.css to assets/css/extended/

.post-footer h3 {
    margin: 1.2em 0 1.2em;
}

.post-footer .related {
    padding-bottom: 1.5rem;
}

.post-footer .related ul {
    padding-inline-start: 20px;
}

.post-footer .related ul li {
    list-style-type: square;
    margin-bottom: 0.5rem;
}

.post-footer .related ul li a {
    text-decoration: underline;
    transition: color 0.3s ease-in-out, text-decoration-color 0.3s ease-in-out;
    text-decoration-thickness: 1px;
}

.post-footer .related-date {
    font-size: 0.8em;
    font-style: italic;
}

.post-footer .related a:hover {
    box-shadow: 0 1px;
    text-decoration: none;
    color: #ff5722;
    text-decoration-color: #ff5722;
    text-decoration-thickness: 2px;
}

3.Adjustments to single.html

Adjust layouts/_default/single.html and insert the code underneath the <footer class="post-footer">:

<footer class="post-footer">
  {{- if (.Param "ShowRelatedContent") }}
    {{- partial "related.html" . }}
  {{- end }}
  {{- $tags := .Language.Params.Taxonomies.tag | default "tags" }}
  <ul class="post-tags">
    {{- range ($.GetTerms $tags) }}
    <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
    {{- end }}
  </ul>
  {{- if (.Param "ShowPostNavLinks") }}
  {{- partial "post_nav_links.html" . }}
  {{- end }}
  {{- if (and site.Params.ShowShareButtons (ne .Params.disableShare true)) }}
  {{- partial "share_icons.html" . -}}
  {{- end }}
</footer>

4.Translation labels

In i18n/en.yaml add the following code

- id: related
  translation: "Related posts"

In i18n/zh.yaml add the following code

- id: related
  translation: "相关文章"

5.Add the hugo.yaml parameter

hugo.yaml

params:
    showRelatedContent: true