You need to enable JavaScript to run this app. Jekyll 中如何统计并展示作者列表页每位作者的文章数?
优惠活动
大模型
产品
解决方案
定价
更多

Jekyll 中如何统计并展示作者列表页每位作者的文章数?

Jekyll 站点作者列表页实现作者文章数量统计的方案

所有实现逻辑完全基于Jekyll原生Liquid语法,无需安装第三方插件,适配GitHub Pages等所有不支持自定义插件的静态托管环境。

前置准备

  • 确保所有文章的Front Matter中配置了格式统一的作者标识,比如每篇文章头部设置author: 作者唯一标识,避免同一作者因名称写法不一致导致的统计遗漏。
  • 作者的基础信息(显示名、简介等)推荐统一存储在_data/authors.yml文件中,后续调整作者信息时不需要修改页面代码,维护成本更低。

核心实现代码

在你的作者列表页(可以是独立的authors.md页面、或者对应布局文件)写入以下遍历逻辑,即可实现要求的展示效果:

{% comment %} 遍历_data目录下配置的全部作者 {% endcomment %}
{% for author in site.data.authors %}
  {% assign author_id = author[0] %}
  {% assign author_profile = author[1] %}
  {% assign post_total = 0 %}

  {% comment %} 匹配站点内所有属于当前作者的文章 {% endcomment %}
  {% for post in site.posts %}
    {% if post.author == author_id %}
      {% assign post_total = post_total | plus: 1 %}
    {% endif %}
  {% endfor %}

  {% comment %} 按要求格式输出列表项 {% endcomment %}
- {{ author_profile.display_name }} - {{ post_total }} article{% if post_total != 1 %}s{% endif %}
{% endfor %}

渲染后即可直接生成目标展示效果:

Author 1 - 4 articles
Author 2 - 3 articles
Author 3 - 5 articles

常见问题与调整说明

  • 统计范围自定义:上述代码默认仅统计_posts目录下的正式发布文章。如果需要把自定义文档集合、静态页面也纳入统计范围,只需要把内层遍历的site.posts替换为对应集合的对象,比如site.docs、site.pages即可。
  • 无_data配置的适配:如果你的作者列表没有存在_data目录、是直接硬编码在页面上的,可以把外层的作者遍历逻辑替换为自定义列表:
    {% assign author_list = "Author 1,Author 2,Author 3" | split: "," %}
    {% for author in author_list %}
      {% assign post_total = 0 %}
      {% for post in site.posts %}
        {% if post.author == author %}
          {% assign post_total = post_total | plus: 1 %}
        {% endif %}
      {% endfor %}
    - {{ author }} - {{ post_total }} article{% if post_total != 1 %}s{% endif %}
    {% endfor %}
    
  • 统计值不准排查:如果出现计数和实际文章数不符的情况,优先检查两点:一是文章Front Matter里的作者标识和作者配置的id是否完全匹配(注意大小写、多余空格这类细节);二是对应文章是否设置了published: false的草稿状态,这类文章默认不会被site.posts遍历到。
  • 性能说明:代码采用双层遍历逻辑,对绝大多数文章量在千篇以内的个人站点来说,渲染耗时完全在可接受范围,不需要额外做性能优化。

内容的提问来源于stack exchange,提问作者Richard Kacerek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.31 01:15:00