【问题标题】:WordPress ACF Repeaters with Timber/Twig带有木材/树枝的 WordPress ACF 中继器
【发布时间】:2020-06-19 12:46:00
【问题描述】:

在 Google 上搜索了很多,但找不到解决方案。我是 Twig/Timber 的新手,但我有很多 Laravel 经验,所以它很相似,但我不熟悉如何在 Twig 模板中使用 ACF Repeater 字段。

我已经完成了 ACF 中继器字段设置:

中继器名称:联系人

在中继器中我有:

Contact Name: contact_name

Contact E-mail Address: contact_email_address

Contact Phone Number: contact_phone_number

这是我的 Twig 文件中当前“不起作用”的内容:

{% if post.contact %}
  <h3>Contacts</h3>
  {% for contact in post.contact %}
    <p><strong>Name:</strong> {{ post.contact_name }}</p>
    <p><strong>E-mail:</strong> {{ post.contact_email_address }}</p>
    <p><strong>Phone:</strong> {{ post.contact_phone_number }}</p>
  {% endfor %}
{% endif %}

但没有骰子。我知道post.contact 正在工作,因为它正在显示&lt;h3&gt;Contacts&lt;/h3&gt;。我更担心 foreach 似乎没有按预期工作。

感谢您的帮助或指导。

更新:我查看了文档并尝试了:

{% for contact in post.contact('contact') %}

但这似乎也没有任何效果。

【问题讨论】:

    标签: wordpress twig advanced-custom-fields timber


    【解决方案1】:

    解决方法:

    {% if post.contact %}
      <h3>Contacts</h3>
      {% for contact in post.meta('contacts') %}
        <p><strong>Name:</strong> {{ contact.contact_name }}</p>
        <p><strong>E-mail:</strong> {{ contact.contact_email_address }}</p>
        <p><strong>Phone:</strong> {{ contact.contact_phone_number }}</p>
      {% endfor %}
    {% endif %}
    

    【讨论】:

    • 如果你像在开篇文章中那样循环,这应该可以在没有 post.meta 行的情况下工作:{% for contact in post.contacts %} 在某些情况下我需要使用:{% for item在 post.get_field('repeatet_fieldname') %}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2021-11-20
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多