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

发布于 2024-11-10 09:47:19 字数 669 浏览 8 评论 0原文

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

I'm trying to set up Jekyll so that a quote from the first post in the list of posts is displayed in a sidebar, but I can't quite work out how to do it. I have the quote text defined as a quote variable within the YML Front Matter in each post's Markdown.

This is the relevant extract from my default.html:

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

And this is my 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 %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷子口的你 2024-11-17 09:47:19
{% 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 问题中存储引用,但这应该得到您想要的。

{% 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 %}

and in default.html

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

I don't think you can store references in the YML matter but this should get what you want.

不奢求什么 2024-11-17 09:47:19

经过多次实验,我能够使用 default.html 中的 Liquid 代码片段解决问题:

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

After much experimentation, I was able to solve the problem using this Liquid snippet within default.html:

<div id="sidebar">
  <blockquote>
  {% if page.quote %}
    {{ page.quote }}
  {% else %}
    {{ site.posts.first.quote }}
  {% endif %}
  </blockquote>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文