ACF variables in blade template

I can’t say this is the most elegant way of doing it but perhaps something like:

Controller

public function related()
{
    global $post;

    $data = [];

    $related_posts = get_field('related_posts');

    foreach ($related_posts as $p) {

        $this_post = (object) array(
            'thumbnail' => get_the_post_thumbnail($p, 'thumbnail', array('class' => 'card-img-top img-fluid')),
            'permalink' => get_the_permalink($p),
            'title' => get_the_title($p),
        );

        array_push($data, $this_post);
    }

    return $data;
}

View

@foreach ($related as $p)

    {{ $p->thumbnail }}
    {{ $p->permalink }}
    {{ $p->title }}

@endforeach

Totally untested and assumes that your returning your related post as the post ID not an object (by looking at what you posted up). It involves two loops (one in the controller and one in the view) but it keeps the view very tidy in my opinion.

5 Likes