【问题标题】:Jekyll shows excerpt only on one postJekyll 仅在一篇文章中显示摘录
【发布时间】:2015-08-18 13:29:38
【问题描述】:

我试图在首页上显示帖子的摘录,但它似乎只适用于一篇帖子。摘录是这样插入的:

{% for post in paginator.posts %}
    <div class="unit whole single-post-excerpt">
        <h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
        <p class="description">{{ post.excerpt | strip_html }}</p>
    </div>
{% endfor %}

帖子长这样,第一个叫"2015-08-17-first-post.markdown"

---
layout: post
title: "The first post that works"
---
This is the first post that has a working excerpt.

That was the excerpt, and this one won't show up in the list of posts.

These two paragraphs are shown on the post page instead.

第二个,叫"2015-08-18-second-post.markdown"

---
layout: post
title: "The second post that does not work"
---
This is the second post that does not work as intended.

This paragraph gets also added to the excerpt.

As does this one.

为什么第二篇文章的摘录没有按预期工作?我是否错过了配置中的某些内容或..?我还尝试添加更多帖子,并且摘录仅在第一个帖子上正常工作。所有后续帖子都有帖子列表中显示的所有文本,没有任何段落格式 - 都只是塞进一个段落。

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    您需要一个摘录后分隔符。

    正如 Jekyll 文档显示的 post-excerpts

    因为 Jekyll 抓住了第一段,您不需要将摘录包裹在 p 标签中,这已经为您完成了。

    然后如果你继续阅读......

    如果您不喜欢自动生成的帖子摘录,可以通过在帖子的 YAML Front Matter 中添加 excerpt 值来明确覆盖它。或者,您可以选择在帖子的 YAML 前端定义自定义 excerpt_separator

    ---
    excerpt_separator: <!--more-->
    ---
    
     Excerpt
     <!--more-->
     Out-of-excerpt
    

    您还可以在 _config.yml 配置文件中全局设置 excerpt_separator。

    您的帖子需要分隔符,以便手动告诉 Jekyll 在哪里显示帖子,以及在哪里截断文本。默认应该是&lt;!--more--&gt;,所以继续把它放在你的第二个帖子中。如果您想使用自定义的,只需转到您的_config.yml 并添加excerpt_separator: &lt;!-- what you want here --&gt;。摘录通常采用 cmets 的形式,因此读者看不到它们。

    【讨论】:

    • 问题是 Jekyll 不会产生自动第一段作为摘录。我实际上喜欢“第一段作为摘录”的东西,它只是不这样做。我认为这是我下载的主题的问题,因为默认主题不会导致这种奇怪的行为。
    最近更新 更多