如何显示一个post的预览? (使用Jekyll Bootstrap主题)

这可能是一个简单的问题,但是如何在默认页面上显示我的post的预览? 我正在使用Jekyll Bootstrap主题汤姆。

通过这里的function看,我发现strip_html和truncatewords。

这里有一个包含75个预览文字的“post列表”示例。

<ul > {% for post in site.posts limit 4 %} <li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></li> {{ post.content | strip_html | truncatewords:75}}<br> <a href="{{ post.url }}">Read more...</a><br><br> {% endfor %} </ul> 

这也起作用至less1.0.0 ,内置和使用简单。

 <ul> {% for post in site.posts %} <li> <a href="{{ post.url }}">{{ post.title }}</a> <p>{{ post.excerpt }}</p> </li> {% endfor %} </ul> 

看到这里 。

我喜欢WordPress的<!--more-->评论的方法,所以我写了一些东西:

_plugins / more.rb:

 module More def more(input, type) if input.include? "<!--more-->" if type == "excerpt" input.split("<!--more-->").first elsif type == "remaining" input.split("<!--more-->").last else input end else input end end end Liquid::Template.register_filter(More) 

您的文章将如下所示:

 --- layout: post title: "Your post title" published: true --- <p>This is the excerpt.</p> <!--more--> <p>This is the remainder of the post.</p> 

然后你可以在你的模板中使用它:

显示摘录( <!--more-->注释之上的所有内容):

 <summary>{{ post.content | more: "excerpt" }}</summary> 

显示余数(在<!--more-->注释之后的所有内容):

 <article>{{ post.content | more: "remaining" }}</article> 

任何除了excerptremaining参数都将显示整个post。

这是一个古老的问题,仍然想增加一个格式问题的解决方法,如在@ Talon876的答案在这里提出的 。

在每篇文章的结尾添加结束标签(如</em>,</strong> or </b>可能不那么整齐,但在显示摘录时会保持格式。

例如:

 <ul > {% for post in site.posts limit 4 %} <li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></li> {{ post.content | strip_html | truncatewords:75}} </em></strong> <!-- This is what does the task--> <br> <a href="{{ post.url }}">Read more...</a><br><br> {% endfor %} </ul>