在我的博客文章索引中,我想从文章中获取第一张图片,以仅使用液体将其显示在索引中,以便在github页面上使用。
我有分裂的感觉,但是我对液体不满意。
我希望能够获取图像网址并将其放在变量中以对其进行样式设置。
理想的解决方案是:
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">{{post.content | first_image}}</a>
</li>
{% endfor %}
有任何想法吗?
得到它的工作。不确定它将如何缩放,但是此流动代码在所有帖子中循环,并从帖子中获取第一张图片的来源并显示该帖子。我用多张图像对其进行了测试,并且可以正常工作。
<ul>
{% for post in site.posts %}
<li>
{% assign foundImage = 0 %}
{% assign images = post.content | split:"<img " %}
{% for image in images %}
{% if image contains 'src' %}
{% if foundImage == 0 %}
{% assign html = image | split:"/>" | first %}
<img {{ html }} />
{% assign foundImage = 1 %}
{% endif %}
{% endif %}
{% endfor %}
<a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句