Jacek
June 26, 2020, 10:23am
1
Hi all, I try figure out how to return ACF blocks in one template but in two sections.
Right now I have few blocks. In one section (one row) I have sidebar and two blocks and below I want to return other blocks but in full width without columns.
I was thinking about adding custom field true/false to these blocks, and If block is set to true then return to child partials. But I’am stuck and I’m not sure that’s a good idea
<div class="grid-container">
<div class="grid-x">
<div class="cell medium-4 show-for-medium">
@include('partials/sidebar-category')
</div>
<div class="cell medium-8">
@include('partials.content-page')
</div>
</div>
<div class="grid-x">
<div class="cell">
@include('partials.content-page-child')
</div>
</div>
</div>
Jacek
June 30, 2020, 9:58pm
2
I found solution to parse and render blocks, but it not flexible solution because ( I can’t change location blocks in admin panel ) but works.
Jacek
July 1, 2020, 1:43pm
3
I will share my code, maybe that will be helpfully for somebody in the future. It’s not perfect but works in this case.
In acf block add “true/false” field for each blocks (in this example with name “widget_section”).
In controller for page add below code (in this example FrontPage).
class FrontPage extends Controller
{
/* Array with filtered meta data */
private function storeArray()
{
$store_blocks = [];
$top_section = [];
$bottom_section = [];
}
/* Store logic */
public function storeLogic()
{
global $post;
$blocks = parse_blocks( $post->post_content );
/* store_blocks array */
foreach($blocks as $block) {
if(isset($block['attrs']['data']['widget_section'])) {
$this->store_blocks[] = $block;
}
}
/* top_section array */
foreach($this->store_blocks as $item_top) {
if($item_top['attrs']['data']['widget_section'] == 1 ) {
$this->top_section[] = $item_top;
}
}
/* bottom_section array */
foreach($this->store_blocks as $item_bottom) {
if($item_bottom['attrs']['data']['widget_section'] == 0 ) {
$this->top_bottom[] = $item_bottom;
}
}
}
/* Top section */
public function blockTop()
{
$top_array = $this->top_section;
return array_map(function ($post) {
return [
'item_top' => apply_filters( 'the_content', render_block( $post ) ),
];
}, $top_array);
}
/* Bottom section */
public function blockBottom()
{
$bottom_array = $this->top_bottom;
return array_map(function ($post) {
return [
'item_bottom' => apply_filters( 'the_content', render_block( $post ) ),
];
}, $bottom_array);
}
}
Next in your home template, you can return any where blocks with fields set on True
@foreach($block_top as $item)
{!! $item['item_top'] !!}
@endforeach
And for blocks which doesn’t have set field on True
@foreach($block_bottom as $item_b)
{!! $item_b['item_bottom'] !!}
@endforeach
system
Closed
August 7, 2020, 10:23am
4
This topic was automatically closed after 42 days. New replies are no longer allowed.