I’m doing exactly what you’re doing in several themes (I just double-checked my code) and it’s working. Are you sure your field is named featured_article ?
If you add print_r($p) right after your foreach does it give you info about the featured article?
I got it to work with the last code block I posted using the Relationship ACF Field Type and your tip to use the Basic loop method. However, the Post Object keeps displaying the Home page. That’s ok though, I can use the Relationship Field Type for the purpose of this page and I got it to work with controller/blade syntax as well. Thanks so much for the help!
Hi there, do you know when Controller 2.0.0 is going to be used by Sage as a default?
Are there any potential issues with updating this manually in order to get the most out of the ACF module you’ve so brilliantly created? As suggested in this thread: Setting up a controller for ACF fields
How will this be affected by future updates to Sage?
Easy way would be to use get_field and store the flexible field data into a variable and then foreach over it to access the data you need (in your controller).
Is there a way to include partials in the controller while using flexible content field, not just single field data?
What I’m trying to do is to have the usual acf check for the flexible content field in the controller, get the corresponding partial and then display the whole thing in the view file, while each partial can show its own sub fields.
Basically I’m trying to make a simple page layout builder in the back-end and I use the flexible content field for that. There would be these pre-made page sections like a page-banner, or a basic text content with title or a gallery, etc. And I want to make all these as individual partials. Just can’t really figure out how to make the controller spit it out on the page.
Not sure if I tackled the problem from the right direction, I just started learning the new sage and blade templating in general a few hours ago. I tried to avoid doing it with my old way which is having the acf if/while checks then including the specific partial in the page template file.
I am also doing the same thing. I have gotten to the point where I can display different content per ACF shelf but it does not utilize the controller.
i did have a solution where i could use multiple ACF controllers on any page; however, when i wanted to reuse a flexible content shelf on the same page, it would return the value of the first content row.
Example:
Home Page : Flexible Content : Row 1 - Content, Row 2 - Image, Row 3 - Content. The values of the variables from the content controller - Row 1 and Row 3 - are the same on the front end, but on the back end they are different.
I ended up putting together something based on nathobson’s tips in the controller, but let the partial handling in the view file. That wasn’t my original goal but I thought that much logic is okay(or necessary) since it is the same how it is in the default pre-made files.
controller:
public function getLayouts()
{
$layouts = get_field('layout');
$data = [];
if($layouts):
foreach($layouts as $layout):
// common layout attribute settings
$this_layout = (object)[
'background_color' => $layout['settings']['background_color'],
'background_image' => $layout['settings']['background_image'],
'custom_class' => $layout['settings']['custom_class']
];
// specific attributes based on layout type
if($layout['acf_fc_layout'] == 'large_carousel'):
$this_layout->type = 'large_carousel';
$this_layout->slides = $layout['carousel_slides'];
elseif($layout['acf_fc_layout'] == 'text_section'):
$this_layout->type = 'text_section';
$this_layout->full_width = $layout['settings']['full_width'];
endif;
array_push($data, $this_layout);
endforeach;
return $data;
endif;
}
View:
@foreach($get_layouts as $layout)
@if($layout -> type == 'large_carousel')
@include('partials.page-section-carousel')
@elseif ($layout -> type == 'text_section')
@include('partials.page-section-text')
@endif
@endforeach
Inside a partial a you can get the values you set up for the object with $layout -> somestuffyouneed.
It isn’t much more efficient than the regular acf while loop but at least the flexible content data is available in the controller now. Not sure if it helps in your case but maybe you can get some ideas and improve my mess.
edit: Oh and I used this for debug purposes in the loop to check how ACF made the structure:
it’s there in the code examples really. In the controller I build an array of layout objects from the acf input and in the view it goes through the array and displays the corresponding layout template. It doesn’t matter how many times you put in the same template in the back end because they become different objects in the controller.
There are some stuff there that is specific to my case but the main part is the $this_layout which is current object that will be put into the $data array.
public function getLayouts()
{
//acf flexible content field
$layouts = get_field('layout');
//this will hold all the layouts
$data = [];
//go through each layouts that got put in there
if($layouts):
foreach($layouts as $layout):
//$this_layout is the current one
$this_layout = (object)[
//the layout object's title value will be the 'title' named acf sub field of the flexible content layout
$this_layout->title = $layout['title'];
];
// if you want to add specific fields based on flexible content layout type, you can check it this way
if($layout['acf_fc_layout'] == 'text_section'):
//the layout object's type value is going to be 'text section'
$this_layout->type = 'text_section';
endif;
//put the current object into the $data array
array_push($data, $this_layout);
endforeach;
//return the $data array with all the flexible content objects in it
return $data;
endif;
}
view file
@extends('layouts.custom')
@section('content')
@while(have_posts()) @php the_post() @endphp
//get_layouts function from the controller and run through each
@foreach($get_layouts as $layout)
//if the object's type value is 'text_section' then do this
@if ($layout -> type == 'text_section')
<section class="page-section standard-text-section">
<div class="container">
<div class="text-wrapper">
//display one of the sub field values we set up in the controller this way
<h1>{{$layout -> title}}</h1>
<p>{{$layout -> type}}</p>
</div>
</div>
</section>
@endif
@endforeach
@endwhile
@endsection
First of all, thanks for this post as we’re able to use this to have clean templates!
If one were trying to return the default WP thumbnail cropped image size from an ACF image field in a repeater field, what’s the best way to do that? Also, how to correctly grab the alt tag for the image?