杰基尔条件图像标签在不存在时仍然显示



>我正在为我的网站使用 Jekyll,我有条件的附加图像,以 {% if page.second-image %} 开头并以 {% endif %} 结尾。 不幸的是,即使帖子中没有第二张图片,这些图像标签仍然显示。 如果它们没有编码到当前帖子中,我如何让 Jekyll 忽略它们? 谢谢!!

layout: post
title: title
date: 2015-09-02
image: /img/path.png
second-image: /img/path.png
third-image: /img/path.png
description: Landing Page/Menu Design
meta-title: meta title information

但是帖子模板中的第 4、第 5 和第 6 张图片显示为断开的链接,即使它们不存在

好吧,试试这个:

---
title: "My title" # wrap in double quotes
image: path.png # remove source folder
second-image: path.png
description: "My description" # wrap in double quotes
meta-title: "meta title information" # wrap in double quotes
... # whatever you need
---

然后,要post.html(或您从中调用帖子的另一个 html 文件):

{% if page.second-image %} <img class="some_class" src="{{ site.baseurl }}/img/{{ page.second-image }}" alt="{{ page.title }}"> {% endif %} 
<小时 />

或:

---
extra_imgs: [path1.png, path2.png, path3.png]
---

然后

{% if page.extra_imgs %} 
{% for img in page.extra_imgs %}
<img class="some_class" src="{{ site.baseurl }}/img/{{ img }}" alt="{{ page.title }}"> 
{% endfor %}
{% endif %}

提示:避免在 yaml 前额值中使用特殊字符。他们可以在他们之后破解代码!无论如何,如果使用双引号,您应该没问题。

让我知道它是怎么回事!

希望对您有所帮助!

最新更新