dillonzq / LoveIt

❤️A clean, elegant but advanced blog theme for Hugo 一个简洁、优雅且高效的 Hugo 主题
https://hugoloveit.com
MIT License
3.38k stars 1.07k forks source link

[QUESTION] How can I modify the home page to display series #727

Closed SingularisArt closed 2 years ago

SingularisArt commented 2 years ago

Describe the feature you want 描述你的功能需求

I'm making a ton of series about different things and I'd like the home page to display only an introduction to each series. For example, I'm making a series on using LaTeX for notes, setting up NeoVim from scratch with Lua, Python Crash Course. How can I make the home page only display something like:

Using LaTeX for Notes Setting up NeoVim From Scratch with Lua Python Crash Course ...

Here's an example. The links displayed first are intros to his series. If you don't understand, please, ask questions! :-)

SingularisArt commented 2 years ago

Here's the modified version of the index.html file in layouts/.

{{- define "content" -}}
  {{- $params := .Scratch.Get "params" -}}
  {{- $profile := .Site.Params.home.profile -}}
  {{- $posts := .Site.Params.home.posts -}}

  <div class="page home"{{ if ne $posts.enable false }} data-home="posts"{{ end }}>
    {{- /* Profile */ -}}
    {{- if ne $profile.enable false -}}
      {{- partial "home/profile.html" . -}}
    {{- end -}}

    {{- /* Content */ -}}
    {{- if .Content -}}
      <div class="single">
        <div class="content" id="content">
          {{- dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
        </div>
      </div>
    {{- end -}}

    {{- /* Posts */ -}}
    {{ $series_intro := slice "series intro" }}
    {{ $series_intro_posts := where .Site.Pages ".Params.tags" "intersect" $series_intro }}

    <h1>My Top 5 Favorite Series</h1>

    <hr>

    {{ range first 5 $series_intro_posts }}
      {{- if ne $posts.enable false | and .Site.RegularPages -}}
        {{- /* Paginate */ -}}
        {{- $pages := where .Site.RegularPages "Type" "posts" -}}
        {{- if .Site.Params.page.hiddenFromHomePage -}}
          {{- $pages = where $pages "Params.hiddenfromhomepage" false -}}
        {{- else -}}
          {{- $pages = where $pages "Params.hiddenfromhomepage" "!=" true -}}
        {{- end -}}
        {{- with $posts.paginate | default .Site.Params.paginate -}}
          {{- $pages = $.Paginate $pages . -}}
        {{- else -}}
          {{- $pages = .Paginate $pages -}}
        {{- end -}}
        {{- .Render "summary" -}}
      {{- end -}}
    {{ end }}

    View the rest <strong><a href="/tags/series-intro">here</a></strong>.

    <h1>My Top 5 Favorite Posts</h1>

    <hr>

    {{ $favorite := slice "favorite" }}
    {{ $favorite_posts := where .Site.Pages ".Params.tags" "intersect" $favorite }}
    {{ $favorite_display_posts := $favorite_posts }}

    {{ range first 5 $favorite_display_posts }}
      {{- if ne $posts.enable false | and .Site.RegularPages -}}
        {{- /* Paginate */ -}}
        {{- $pages := where .Site.RegularPages "Type" "posts" -}}
        {{- if .Site.Params.page.hiddenFromHomePage -}}
          {{- $pages = where $pages "Params.hiddenfromhomepage" false -}}
        {{- else -}}
          {{- $pages = where $pages "Params.hiddenfromhomepage" "!=" true -}}
        {{- end -}}
        {{- with $posts.paginate | default .Site.Params.paginate -}}
          {{- $pages = $.Paginate $pages . -}}
        {{- else -}}
          {{- $pages = .Paginate $pages -}}
        {{- end -}}
        {{- .Render "summary" -}}
      {{- end -}}
    {{ end }}

    View the rest <strong><a href="/tags/favorites">here</a></strong>.

  </div>
{{- end -}}

This will create two sections. One for your favorite series and one for your favorite posts. To make the post a series or your favorite, you have to add the tag: series to a blog post, which makes it a series. To make it display in the series section, you need to add the tag: series intro tag to your post, which makes it the introduction post to the series. To make the post a favorite, just add the tag: favorite to your blog post, which makes it a favorite. Only 5 will be displayed at a time.