Ad

Issue With Post Object Timber/Twig WordPress

- 1 answer

I have an ACF field, It's a Repeater, and inside the Repeater is a Post Object.

Controller

/* Template Name: Strategic Partners */

use Timber\Timber;
use Timber\PostQuery;
use Flynt\Utils\Options;

use const Flynt\Archives\POST_TYPES;

$context = Timber::get_context();

$context['slider'] = get_field('slider');
$context['featured'] = get_field('featured');

if (isset($_GET['contentOnly'])) {
    $context['contentOnly'] = true;
}

Timber::render('templates/StrategicPartners/index.twig', $context);

Here's the snippet from the View

{% for partner in Post(featured) %}
  {{ partner.link }}
{% endfor %}

I'm var_dumping the context, and it's getting the Timber Post... but {{ partner.title }} does not grab the Post Title, it's blank.

{{ partner.link }} comes out with the direct link.

Output

http://***/strategic-partners/
http://***/strategic-partners/

What am I doing wrong here? I'm using {{ Post() }}} within a seperate repeater, within for foreach itself and that works on another page, but it does not work here either, so I tried Post() included in the foreach and it still is not working.

Ad

Answer

No one answered this for me. I answered it myself.

There isn't much documentation on this within the formal Timber documentation so I hope this helps somebody.

Do not call Post with your foreach

Do this for a Post Object within a Repeater (ACF)

{% for item in seq %}
 {{ Post(item.post_object_name).field }}
{% endfor %}

seq is your ACF Field

post_object_name is the Post Object within the field.

field is the field within the Post Object type.

Hope this helps someone.

For more Context:

Controller

<?php

/* Template Name: Strategic Partners */

use Timber\Timber;
use Timber\PostQuery;
use Flynt\Utils\Options;

use const Flynt\Archives\POST_TYPES;

$context = Timber::get_context();

$context['slider'] = get_field('slider');
$context['featured_sps'] = get_field('featured_strategic_partners');
$context['rebate'] = get_field('rebate_information');

if (isset($_GET['contentOnly'])) {
    $context['contentOnly'] = true;
}

Timber::render('templates/StrategicPartners/index.twig', $context);

Twig View

{% for partner in featured_sps %}
  {{ Post(partner.featured).title }}
{% endfor %}

Images are more complex

{% for partner in featured_sps %}
  <div class="slide">
    <a target="_blank" rel="nofollow noreferrer" href="{{ Post(partner.featured).link }}">
      <img src="{{ Image(Post(partner.featured).logo).src }}" alt="{{ Post(partner.featured.title) }} Logo">
    </a>
  </div>
{% endfor %}
Ad
source: stackoverflow.com
Ad