使用木材从 ACF 中继器获取第一行 [英] Get first row from ACF Repeater using Timber

查看:22
本文介绍了使用木材从 ACF 中继器获取第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ACF 中继器字段,它在 wordpress 中使用 Timber 重复图像.我的代码是...

I have an ACF repeater field which repeats out images using Timber in wordpress. My code is as...

{% for item in item.get_field('product_images')}} 
{{TimberImage(item.product_image).src('thumbnail')}}
{% endfor %}

非常简单,这将按预期返回我添加到此转发器的所有行,但我只想返回第一行,即转发器中的第一张图像.

Pretty simple and this returns all the rows I have added to this repeater as expected, but I only want to return the first row, i.e. the first image in the repeater.

我看到您可以使用 ACF 语法简单地跳出循环,但就是无法弄清楚使用 Timber 可以做到这一点.任何想法都会很棒.谢谢.

I see you can simply break out of the loop using the ACF syntax but just can't figure out it its possible to do using Timber. Any ideas would be great. Thanks.

推荐答案

好吧,方法略有不同.在进一步阅读 twig 文档后,我遇到了 循环变量

Ok, slightly different approach. Upon reading twig docs further I came across loop-variables

所以我们可以这样做...

So we can do this...

{% for item in item.get_field('product_images') %}
   {% if loop.first %}
      <img src="{{TimberImage(item.product_image).src('thumbnail')}}" alt="{{item.product_title}}">
   {% endif %}  
{% endfor %}

并且只检查第一行并显示它.轻松愉快.

And check only for the first row and display it. Easy-peasy.

这篇关于使用木材从 ACF 中继器获取第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆