使用Jekyll显示第一篇文章中的引用



我正在尝试设置Jekyll,以便在侧边栏中显示帖子列表中第一篇帖子的引用,但我不太清楚如何做到这一点。我在每个帖子的Markdown中,将引用文本定义为YML Front Matter中的quote变量。

这是我的default.html:的相关摘录

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ page.quote }}</blockquote>
</div>    

这是我的index.html:

---
layout: default
quote: ** Can a variable referencing the first post go here? **
---
{% for post in site.posts limit:10 %}
  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}
{% for post in site.posts limit:10 %}
  {% if forloop.index0 == 0 %}
    {% assign quote = post.quote %}
  {% endif %}
  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}

并且在default.html

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ quote }}</blockquote>
</div> 

我不认为你可以在YML事件中存储引用,但这应该得到你想要的。

经过大量实验,我能够在default.html:中使用这个Liquid片段来解决这个问题

<div id="sidebar">
  <blockquote>
  {% if page.quote %}
    {{ page.quote }}
  {% else %}
    {{ site.posts.first.quote }}
  {% endif %}
  </blockquote>
</div>

最新更新