I'm creating an options page so I can order entries in a custom post type. For some reason I can loop through the entries selected(the elements appear in the DOM), but I can't get the data from them -aka the title, link, image, etc.
$context['service_options'] = get_field('service_pages', 'options');
Timber::render( $templates, $context );
{% for post in service_options %}
{{post.id}} {{ post.title }}
<a class="service-card grid-item" href="{{post.link}}" >
<div class="image-wrapper">
<h2>{{post.title}}</h2>
<img src="{{ Image(post.meta('listing_image')).src }}">
<div class="overlay">
</div>
</div>
</a>
{% endfor %}
I take a guess and say that your service_pages
field is an ACF relationship fields. In the field settings, you can tell ACF to either return post IDs or full post objects. When you return post objects, the objects will be instances of WP_Post
, but you would need instances of Timber\Post
for all the functionality to work. Here’s how you can set that up.
In PHP:
$service_options = get_field( 'service_pages', 'options' );
$service_options = array_map( function( $page ) {
// Convert whatever $page is to a Timber\Post.
return new Timber\Post( $page );
}, $service_options );
$context['service_options'] = $service_options;
Timber::render( $templates, $context );
In Twig, it might even be easier:
{% for post in Post(service_options) %}
{{post.id}} {{ post.title }}
{# ... #}
{% endfor %}
Here, the Post()
function will convert everything that’s in the service_options
array to Timber Posts.
For ordering posts for a Custom Post Type – instead of using a separate relationship field – you could also use one the following plugins to directly order posts in the posts list view: