【问题标题】:Displaying a quote from the first post using Jekyll使用 Jekyll 显示第一篇文章的引用
【发布时间】:2024-11-12 13:00:02
【问题描述】:

我正在尝试设置 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 %}

【问题讨论】:

    标签: liquid jekyll


    【解决方案1】:
    {% 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 文件的范围内。
    • 我以前使用过这种技术,所以我有点惊讶。 YML 块中还有quote 吗?您可以尝试将page.quote 更改为quote
    • 不,我在 YML 块中没有 quote。我应该有吗?
    • 看来您必须在 index.html 的 YML Front Matter 中定义 quote 才能在 default.html 中显示它 文件。
    【解决方案2】:

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

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

    【讨论】:

      最近更新 更多