Home › Forums › Add-ons › Flexible Content Field › Need help for correct markup for nested flexible content field
Hello!
I am trying this out now for a couple of hours and now I hope to get some advice.
If somebody could help me and tell me where I made mistakes I would be so happy and it would save me hours!
Here is what I try to build:
Flexible Content (Row)
—-Flexible Content (Left column)
——–WYSIWG Editor
——–Image
—-Flexible Content (right column)
——–WYSIWG Editor
——–Image
Here is my code:
<?php
if( have_rows('reihe') ):
while ( have_rows('reihe') ) : the_row();
if( get_row_layout() == 'reihe' ):
if( have_rows('reihe') ):
while ( have_rows('linke_spalte') ) : the_row();
the_sub_field('linke_spalte_text');
$image = get_sub_field('linke_spalte_bild');
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
endwhile;
// Rechte Spalte
elseif( have_rows('rechte_spalte') ):
while ( have_rows('rechte_spalte') ) : the_row();
the_sub_field('rechte_spalte_text');
$image = get_sub_field('rechte_spalte_bild');
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
endwhile;
//.Rechte Spalte
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
Made a comment in your code, hope it helps
<?php
// check if the flexible content field has rows of data
if( have_rows('reihe') ):
// loop through the rows of data
while ( have_rows('reihe') ) : the_row();
// check current row layout
if( get_row_layout() == 'reihe' ):
// check if the nested repeater field has rows of data
// shoiuldn't this if have rows one 'belinke_spalte'
if( have_rows('reihe') ):
// loop through the rows of data
while ( have_rows('linke_spalte') ) : the_row();
the_sub_field('linke_spalte_text');
$image = get_sub_field('linke_spalte_bild');
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
endwhile;
// Rechte Spalte
elseif( have_rows('rechte_spalte') ):
// loop through the rows of data
while ( have_rows('rechte_spalte') ) : the_row();
the_sub_field('rechte_spalte_text');
$image = get_sub_field('rechte_spalte_bild');
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
endwhile;
//.Rechte Spalte
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
Hello John!
Thanks for your quick reply! I just changed
if( have_rows('reihe') ):
to
if( have_rows('linke_spalte') ):
but unfortunately this doesn’t work eather either. The code I posted at least shows the content of linke_spalte
but not the content of rechte_spalte
.
What I actually miss my self in my markup is the following:
In row 4 I am asking for if( get_row_layout() == 'reihe' ):
For me that seems to be correct because this is the parent flexible content. But later in the code there is no
if( get_row_layout() == 'linke_spalte' ):
or
if( get_row_layout() == 'rechte_spalte' ):
for children flexible content
…
Is this maybe the mistake? In case what is the right markup for it?
this is a markup that works for me
(that has the content-blocks inside sub-flexible fields.
flexible field my_customcontent_width with layouts:
my_customcontent_full and my_customcontent_half_half
flexible field my_customcontent inside my_customcontent_full
with layout text_block
flexible field my_customcontent_lefthalf inside my_customcontent_half_half
with layout text_block
flexible field my_customcontent_righthalf inside my_customcontent_half_half
with layout text_block
)
<?
if( have_rows('my_customcontent_width')):
while ( have_rows('my_customcontent_width') ) : the_row();
if( get_row_layout() == 'my_customcontent_full' ){
if( have_rows('my_customcontent') ):
echo '<div class="full">';
// loop through the rows of data
while ( have_rows('my_customcontent') ) : the_row();
//text block start
if( get_row_layout() == 'text_block' ){
$text = get_sub_field('my_text');
echo $text;
}
//text block end
endwhile;
echo '</div>';
endif;
}
if( get_row_layout() == 'my_customcontent_half_half' ){
echo '<div class="lefthalf">';
while ( have_rows('my_customcontent_lefthalf') ) : the_row();
//text block start
if( get_row_layout() == 'text_block' ){
$text = get_sub_field('my_text');
echo $text;
}
//text block end
endwhile;
echo '</div>';
echo '<div class="righthalf">';
while ( have_rows('my_customcontent_righthalf') ) : the_row();
//text block start
if( get_row_layout() == 'text_block' ){
$text = get_sub_field('my_text');
echo $text;
}
//text block end
endwhile;
echo '</div>';
}
endwhile; endif;
?>
but why use a flexible field with just a single layout inside?
Hello Mediawerk! Thanks for your reply.
I use flexible content fields because I actually want both image and text be optional an later als add other options.
I just tryed your code and it gives me some syntax error and missing expected ;
Sorry but my PHP skills are not that good yet so I can see the mistakes immidiately.
sorry my mistake 😉
i had replaced code with a markup that i use and works. till i found what was wrong with my code that i originally post
Hm, realy strange…
Something must be wrong with the } it gives me syntax error for those…
Are you from Germany by the way?
Wohoo! Thanks it works! I’ll style it a little bit and than post it here so others can use it as a template…
Hey any chance you can post your solution? Having the same issues with syntax errors.
The topic ‘Need help for correct markup for nested flexible content field’ is closed to new replies.
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.