当 Post 没有图像时,如何获取 Jekyll Posts 的缩略图?

How Can You Get Thumbnails for Jekyll Posts When the Post Has No Images?

如果我正在使用 Jekyll 构建博客并且我想分享 link 到 post。我想要 post 的预览缩略图,但是,post 本身没有图像,我想保持这种状态。关于如何做到这一点有什么想法吗?

您可以在每个 post 的前端定义一个 thumbnail 属性,然后在创建包含指向您的 post 的链接的页面时使用它。

这是一个示例博客 post:

---
layout: default
thumbnail: "/path/to/the/thumbnail.png"
---
This is a blog post!

然后在您博客的 index.html 页面中,您将执行如下操作:

{% for post in site.categories.blog %}
    <div>
        <a href="{{ post.url }}" ><img src="{{ post.thumbnail }}" />
        <a href="{{ post.url }}" >{{ post.title }}</a>
    </div>
{% endfor %}

我找到了我需要的解决方案。

Open Graph Protocol 之后,如果 post 有可通过 Facebook、Twitter 等共享的缩略图,您可以像这样指定您的 <head> 开放图元数据。

{% if page.image %}
  <meta property="og:image" content="path/to/post/image.jpg">
{% else %}
  <meta property="og:image" content="path/to/page/image.jpg">
{% endif %}

如果 post 没有指定的图像,它将使用为 页面 指定的图像。然后将以下内容添加到 post 的 front matter:

---
image: path/to/post/image.jpg
---