Ad

WordPress ACF Repeaters With Timber/Twig

Looked around the Google a lot but couldn't find the solution. I'm new to Twig/Timber but I have a lot Laravel experience so it's similar but I'm not familiar with how to work with ACF Repeater fields within Twig templating.

I've got my ACF Repeater Field Setup:

Repeater Name: Contact

Within the Repeater I have:

Contact Name: contact_name

Contact E-mail Address: contact_email_address

Contact Phone Number: contact_phone_number

This is what's currently "not working" in my Twig file:

{% 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 %}

But no dice. I know post.contact is working because it's displaying <h3>Contacts</h3>. I'm more concerened about the foreach that doesn't seem to be working as intended.

Thanks for any help or guidance.

Update: I looked at the documentation and tried:

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

But that didn't seem to have any effect either.

Ad

Answer

Resolved with:

{% 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 %}
Ad
source: stackoverflow.com
Ad