timber: ACF relationship field within repeater stuck in default loop
I’m using ACF and Timber and have a relationship field returning a post object within a repeater field. I want to get data from the related post to display in the template. The repeater field is called like this:
{% for item in post.get_field('gear_recommendations') %}
<div class="item">
<h4>{{item.gear_title}}</h4>
<img src="{{TimberImage(item.gear_photo).src}}" alt="{{item.title}}">
<p>{{item.time_in_episode}}</p>
{# Connect recommendation to a Deal post #}
<p>Related deal: {{TimberPost(item.get_field('gear_to_deal')).title}}</p>
</div>
{% endfor %}
This returns the title of the current post rather than the related one. I did it like this before with PHP which appears to be overriding the default loop somehow with $post:
<?php
$posts = get_field('related_meister');
foreach( $posts as $post): // variable must be called $post
...
Using the debug bar, it looks like ACF is returning the post ID:
[gear_recommendations_0_gear_to_deal] => Array
(
[0] => 106
)
Should I be referencing that ID and getting the post data from there, rather than going directly through the field name? How do I do that? Or is there a way to get out of the default loop so I can get to the relationship data the way I am doing it now?
Help is much appreciated! And thanks so much for your hard work - Timber is so so so awesome.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 15 (6 by maintainers)
@doifeellucky I’ve definitely made that mistake before! In your
href
, make sure you are referring to the link as well not the enture object.I like to set a little Twig variable to store the
post.get_field('related_post')
in for brevity - something like:UPDATE: User error
If I’d used the correct ACF field type, Post Object not Page Link, this would have worked using the following:
<p><a href="{{post.get_field('related_post')}}" target="_blank">{{post.get_field('related_post').title}}</a></p>
Hi
Having done a search, and looked at the docs, this seems to be similar to the problem I’m trying to solve when using ACF.
I’ve created an ACF ‘related_post’ set as a ‘page link’ and want to show the post title (for now) of the related post in the current post.
I’m currently using the following which shows the full URL of the related post as I’d expect.
<p><a href="{{post.get_field('related_post')}}" target="_blank">{{post.get_field('related_post')}}</a></p>
How do I show the related post title? I’ve tried different combinations, from the above and the docs but nothing works. From the original example what are ‘gear_recommendations’? Is this a category or something else?
Thanks
I should have time to do a full exploration tomorrow – for now can you try changing this line and see if this works?
Now…
Change to…
… and see if that makes it work for you?