How to access content in a ACF relationship field within a repeater field

I have a slideshow that is pulling in content from an ACF repeater field. This is working fine and I’ve been able to grab most of the data and display in the template. But I’ve had to add a ACF relationship field within the repeater field that pulls in content from a custom post type.

I’m new to Sage and not the strongest when it comes to PHP.

I’m having trouble working out how to pull out the correct content from the relationship field within my controller. The ‘caseStudy’ is the relationship field.

Here’s my original controller:

public function slideshow() {

    // Initialize the return variable
    $data = '';

    // Get the appropriate hero
    $slideshow = get_field('slideshow_home');

    if(is_array($slideshow)) {

      foreach( $slideshow as $p ) {
          
        $data .= \App\template('partials.front-page.slide-item', [
          'title' => $p['slideshow_home_main_title'],
          'shortTitle' => $p['slideshow_home_short_title'],
          'intro' => $p['slideshow_home_intro'],
          'btnText' => $p['slideshow_home_button_text'],
          'link' => $p['slideshow_home_button_link'],
          'video' => $p['slideshow_home_video'],
          'poster' => $p['slideshow_home_poster'],
          'caseStudy' => $p['slideshow_case_study'],
          ]
        );

      }
    }

    // Always return
    return $data;
  }

I’m using the relationship field by itself elsewhere to get the same sort of content but not inside a repeater field:

public function related() {
        global $post;
    
        $data = [];
    
        $related_case_studies = get_field('featured_case_study');
    
        foreach ($related_case_studies as $p) {
    
            $this_post = (object) array(
                'thumbnail' => get_the_post_thumbnail_url($p, 'full'),
                'permalink' => get_the_permalink($p),
                'title' => get_the_title($p),
            );
    
            array_push($data, $this_post);
        }
    
        return $data;
    }

Do I just need a foreach loop within the caseStudy section?

In my blade template I have an intro-slider with this:

@if( $slideshow )
<div class="intro-slideshow">

{!! $slideshow !!}

</div>
@endif

The a slide-item template with this:

<div class="intro-slideshow__wrapper">

<div class="intro-slideshow__content">

<h1 class="intro-slideshow__title">{{ $title }}</h1>

<p class="intro-slideshow__intro">{{ $intro }}</p>

<p class="intro-slideshow__btn"><a class="btn" href="{{ $link }}>{{ $btnText }}</a></p>
// case study content should go here

</div>
<source src="{{ $video }}" type="video/mp4">

</video>

 </div>

Any help would be greatly appreciated!

John

Going to give myself this as a challenge

If you found a solution please share it though.

One way possibly is to pass the related item to an include and then run a query in that… I’m thinking using something like the @query directive from this GitHub - Log1x/sage-directives: A set of Blade directives for use with Roots Sage.
(I wouldn’t output the whole slideshow in one in that case, I’d loop over the slides in my template)

However if you want to do it all from your controller slideshow array, you’ll just need to build up an entry like you do for your related post . So instead of
'caseStudy' => $p['slideshow_case_study'], you’ll probably want that to be a nested object/array

An example of the latter is here
https://discourse.roots.io/t/acf-repeater-content-within-a-query-loop-array-map/13270/5

I’ve not tried it but it seems to match what I envisaged.

Will give you an update on the above later in the week

@sixheads
what sort of field are you using for the relationship? an actual relationship field or eg a post field?

i think even if you only assign 1 item, in a relationship field you still have to access it as a [0] index

before I give you a solution, I just wanted to check your specific setup

try this:

it might not be the best way, but it should give you what you need. I’m new to Sage and I’ve not used blade for a while. I’m not sure how I’d specifically do it if I sat down and thought about it. I might loop over the slides in the main template rather than just {!! $slideshow !!}. But for now I’ve tried to match your original example

------------------------------------
resources/views/single-foo.blade.php
------------------------------------
@extends('layouts.app')

@section('content')
  {!! $slides !!}
@endsection


------------------------------
app/controllers/single-foo.php
------------------------------

<?php
namespace App;

use Sober\Controller\Controller;

class SingleFoo extends Controller
{

	public function slides() {

	    $data = '';

	    $slidesRepeater = get_field('slides');

	    if(is_array($slidesRepeater)) {

	      foreach( $slidesRepeater as $slide ) {
	          
	        $data .= \App\template('partials.slide', [

                // account for the relationship actually being an array
                // you could always pass the whole array and loop through it 
                // in template with @foreach
                // if you need more than one
	        	'case_study' => $slide['case_study'][0],

	        ]);

	      }
	    }
	    return $data;
	}
}



----------------
slide.blade.php 
----------------

<h2>{{ $case_study->post_title }}</h2>

{{-- apply auto-paragraphs.. I'm not sure the best method here but both seem to work --}}

<div>{!! wpautop($case_study->post_content) !!}</div>
<div>{!! apply_filters('the_content', $case_study->post_content) !!}</div>

you could also just pass the content you need of course

// (inside your loop)
$case_study = (object) [
  'title' => $slide['case_study'][0]->post_title, 
  'body' => $slide['case_study'][0]->post_content 
];

$data .= \App\template('partials.slide', [
   // ... other slide data
   // ...
   'case_study' => $case_study;
]);

then in your slide template

<h1>{{ $case_study->title }}</h1>
<p>{{ $case_study->body }}</p>
1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.