分享一下 hugo 的语法模板,如何获取总文章数和总字数,效果可以参考我的首页。

{{ $articleCount := len .Site.RegularPages }}
{{ $totalWordCount := 0 }}
{{ range .Site.Pages }}
{{ $totalWordCount = add $totalWordCount .WordCount }}
{{ end }}
<p>已经写了 {{ $articleCount }} 篇文章,共 {{ $totalWordCount }} 字。</p>

在首页过滤某个分类。

{{ $pages := .Site.RegularPages }}
{{ $paginator := .Paginate ($pages) }}
{{ $count := 0 }}

{{ range $paginator.Pages }}
{{ $title := .Title }}
{{ $summary := .Summary }}

{{ if and (not (in .Params.categories "weekly")) (lt $count 7) }}
{{ $count = add $count 1 }}

<article class="post-entry">
  <header class="entry-header">
    <h2 class="entry-hint-parent">{{ .Title }}</h2>
  </header>
  <div class="entry-content">
    <p>{{ .Summary }}</p>
  </div>
  <footer class="entry-footer">
    <time datetime="{{ .Date.Format " 2006-01-02 15:04:05 MST" }}">{{ .Date.Format "2006-01-02"}}</time>
    <span class="category">
      {{ range $key, $value := .Params.categories -}}
      {{- if $key }}, {{ end }}
      {{ . }}
      {{- end }}
    </span>
  </footer>
  <a class="entry-link" href="{{ .RelPermalink }}"></a>
</article>
{{ end }}

{{ end }}