Home › Forums › Add-ons › Flexible Content Field › Flexible Content not rendering on frontend
I have the following structure in ACF:
relevant (group)
content_boxes (repeater)
content (flexible content)
new_article (layout 1)
new_html (layout 2)
And my code to display some of it goes like this:
<?php $relevant = get_field('relevant') ?>
<?php $content_boxes = $relevant['content_boxes'] ?>
<?php if ($content_boxes): ?>
<div class="cs-relevant-wrap">
<div class="container">
<h2><?php echo $relevant['heading'] ?></h2>
<div class="relevant-content-list">
<?php foreach ($content_boxes as $content_box): ?>
<div class="content-list-item col-<?php echo $content_box['column_width'] ?>">
<?php if( have_rows('content') ): ?>
<?php while ( have_rows('content') ) : the_row();?>
<?php if( get_row_layout() == 'new_article' ):
$title = get_sub_field('article_title');?>
<h3><?php echo $title ?></h3>
<?php elseif( get_row_layout() == 'new_html' ):
$html_block = get_sub_field('block_editor');?>
<?php echo $html_block; ?>
<?php endif;?>
<?php endwhile;?>
<?php else :?>
<?php endif;?>
</div>
<?php endforeach ?>
</div>
</div>
</div>
<?php endif ?>
It will render up until the flexible content if statements, but acts as if there is no data in the flex content fields (there is). Am i missing something obvious?
You cannot combine
get_field('repeater || group || flexible conentent')
and
have_rows()
when dealing with sub field of any kind.
Everything is in the returned array here
$relevant = get_field('relevant');
and you must deal with looping over that array
$content_boxes = $relevant['content_boxes']
$content_rows = $content_boxes['content'];
foreach ($content_rows as $content) {
if ($content['acf_layout'] == 'new_article') {
$title = $content['article_title'];
}
}
Or you must start with and use have_rows() throughout
if (have_rows('relevant')) {
while (have_rows('relevant')) {
the_row();
if (have_rows('content_boxes')) {
while (have_rows('content_boxes')) {
the_row();
if (have_rows('content')) {
while (have_rows('content')) {
the_row();
}
}
}
}
}
}
Thank you for the reply. I’ve modified my code using the first solution and I think my the only thing that threw me was
if ($content['acf_layout'] == 'new_article') {
$title = $content['article_title'];
}
I don’t think [‘acf_layout’] is meant to be the flexible contents ID, since thats been redefined by a variable earlier on and it doesn’t seem to be an ACF selector. I don’t think I understand what that is meant to be?
going to be honest with you, I’m not sure of the index for the array value that holds the layout off the top of my head. You might want to try outputting the array to make sure.
echo '<pre>'; print_r($content); echo '</pre>';
You were very close!
[‘acf_fc_layout’]
This got it working for me, thanks so much!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.