You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jekyll动态集合列表组件无法访问集合内容问题咨询

问题原因与解决方法

核心问题

你通过{% capture %}和{% assign %}得到的是字符串形式的集合名称,但Jekyll中site.集合名属于静态属性访问语法,无法识别字符串变量。硬编码site.posts1能生效,是因为这是直接调用已知的对象属性,而用变量替代时,Liquid不会自动将字符串解析为site的对应属性。

解决步骤

  1. 改用动态属性访问方式:把静态的点号访问改为方括号访问,Liquid支持用方括号通过字符串变量获取对象属性。
    错误示例(无法遍历):
    {% capture collection_name %}posts1{% endcapture %}
    {% assign target_collection = site.collection_name %} <!-- 这里site不存在collection_name属性 -->
    
    修正后代码:
    {% capture collection_name %}posts1{% endcapture %}
    {% assign target_collection = site[collection_name] %} <!-- 用方括号动态调用集合 -->
    
  2. 正常遍历集合内容:修改后即可像硬编码时一样遍历文章:
    {% for post in target_collection %}
      <h3>{{ post.title }}</h3>
      <p>{{ post.excerpt | truncatewords: 20 }}</p>
    {% endfor %}
    

额外验证点

确保_config.yml中的集合配置正确,示例如下:

collections:
  posts1:
    output: true
    permalink: /posts1/:path/

内容的提问来源于stack exchange,提问作者p-marco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 20:39:19