Home › Forums › Add-ons › Flexible Content Field › Flexible content outputs weird HTML
Hello. I have a flexible content field that uses the “text” and “textfield” fields. But on the fontend page, when I view the source code it seems like the loop is behaving strange. It finds the first field in the loop, then skips all the other. Then it starts over and skips the first field and finds the second, instead of finding both the first and the second field on the first loop.
This gives me this on the front end:
<h1>Headline</h1>
<p></p>
<h1></h1>
<p>textcontent</p>
instead of:
<h1>Headline</h1>
<p>textcontent</p>
Why is this happening?
This is my loop:
<?php if( have_rows('content') ):
while( have_rows('content') ): the_row(); ?>
<h1><?php the_sub_field('headline'); ?></h1>
<p><?php the_sub_field('text_content'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
Thanks
would it make a difference when you use this? :
<?php if( have_rows('content') ):
while( have_rows('content') ): the_row();
$the_headline = get_sub_field('headline');
$the_text_content = get_sub_field('text_content');
?>
<h1><?php echo $the_headline; ?></h1>
<p><?php echo $the_text_content; ?></p>
<?php endwhile; ?>
<?php endif; ?>
Thanks for your answer.
That gave me the same output unfortunately. The thing is, the frontend looks good and as it should, it’s just a shame it should mess up the DOM.
just to get sure:
you have a flexible field with which layouts?
if headline and text_content are separate layouts than it is clear why it do it like that. (because it do for each layout echo headline and text, and at each one there is missing the other field)
if( have_rows('content') ):
while( have_rows('content') ): the_row();
if( get_row_layout() == 'layoutname_of_headline' ){
$the_headline = get_sub_field('headline');
echo '<h1>'.$the_headline.'</h1>';
}
if( get_row_layout() == 'layoutname_of_text_content' ){
$the_text_content = get_sub_field('text_content');
echo '<p>'.$the_text_content.'</p>';
}
endwhile; endif;
Yes, at a second look i saw that I had indeed the headline and the text content in separate layouts. Thanks!
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.