如何显示帖子的预览?(使用Jekyll Bootstrap主题)

dsa*_*ler 39 jekyll

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

Tal*_*876 47

通过这里的函数,我找到了strip_html和truncatewords.

这是一个包含75个预览文字的"帖子列表"示例.

<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>
Run Code Online (Sandbox Code Playgroud)

  • @woozyking这样做不安全.如果截断为75个单词会使您处于某个标记的中间(即"此文本<b>粗体并切断"),则该代码段将呈现您有标记错误.1.0似乎用`post.excerpt`和`except_separator`来解决这个问题.http://stackoverflow.com/questions/10859175/how-to-show-a-preview-of-a-post-using-jekyll-bootstrap-theme/16708341#16708341 (6认同)
  • @mlt只需要拿走`strip_html` (3认同)
  • 它不保留格式.一切都显示为纯文本. (2认同)

kmi*_*ael 45

这也至少可以用于1.0.0,内置并且易于使用.

<ul>
  {% for post in site.posts %}
    <li>
      <a href="{{ post.url }}">{{ post.title }}</a>
      <p>{{ post.excerpt }}</p>
    </li>
  {% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

看到这里.


mat*_*b33 14

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

_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)
Run Code Online (Sandbox Code Playgroud)

你的帖子看起来像这样:

---
layout: post
title: "Your post title"
published: true
---
<p>This is the excerpt.</p>
<!--more-->
<p>This is the remainder of the post.</p>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在模板中使用它,如下所示:

显示摘录(<!--more-->评论上方的所有内容):

<summary>{{ post.content | more: "excerpt" }}</summary>
Run Code Online (Sandbox Code Playgroud)

显示余数(<!--more-->评论后的所有内容):

<article>{{ post.content | more: "remaining" }}</article>
Run Code Online (Sandbox Code Playgroud)

除了excerptremaining将只显示整个帖子的任何论点.

  • 这已经在Jekyll本身实现了:参见[Jekyll:Post Excerpts](http://jekyllrb.com/docs/posts/#post_excerpts).`excerpt_separator`可以在`_config.yml`文件中定义,如:`excerpt_separator:<! - more - >` (4认同)