首页 > 解决方案 > Jekyll 有没有办法检索帖子内容中的图片数量?

问题描述

在 Jekyll 的帖子中,我看到了如何计算单词,include.content | number_of_words但我想知道是否有办法计算内容中的图片?

我确实知道如果我将它添加到 frontmatter 中,有一种方法可以获得特色图片,例如:

feature-img: "img/foobar.png"

根据我的搜索,我确实看到了post.layout,但我无法找到是否有办法获取帖子内容中的图像数量。当我搜索是否有人问过这个问题或者有人在 Jekyll 问题中提出了这个问题时,我唯一没有得到任何结果,但我读过:

我可以看看我是否要为将图像添加到前端的帖子建立一个画廊,例如:

---
layout: post
title: "This is a title"
images:
  - url: "img/foo.png"
    alt: "Enter the foo"
    title: "apart of foo"

  - url: "img/bar.png"
    alt: "Enter the bar"
    title: "apart of bar"
---

但图像遍布整个内容。我想我可以将它硬编码到 frontmatter 中的每个帖子中,例如:

---
image-count: 4
---

但我认为这只会让前面的事情变得臃肿。在 Jekyll 中有没有办法让帖子的图片动态计数?

标签: imagepostjekyll

解决方案


您可能可以拆分内容,如下所示:

{% assign contentparts = content | split: '<img' %}
{% assing amountofimages = contentparts | minus:1 %}

然后使用:

{{ amountofimages }}

请注意,我没有对此进行测试。


推荐阅读