【问题标题】:Is there a way in Jekyll to retrieve the number of pictures in a post's content?Jekyll 有没有办法检索帖子内容中的图片数量?
【发布时间】:2019-06-27 19:20:18
【问题描述】:

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

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

feature-img: "img/foobar.png"

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

我可以看看我是否要为帖子创建一个画廊,将图像添加到 frontmatter 中,例如:

---
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 中,有没有办法让帖子的图片动态计数?

【问题讨论】:

    标签: image post jekyll


    【解决方案1】:

    您可能可以像这样拆分内容:

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

    然后使用:

    {{ amountofimages }}
    

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

    【讨论】:

      【解决方案2】:

      我想出了一种方法来获取帖子中的图像数量,而无需在前面的问题中硬编码图像数量:

      {% assign postPics = include.content | split: "<img" | size %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多