Support

Account

Home Forums Add-ons Flexible Content Field Flexible content outputs weird HTML

Solved

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!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Flexible content outputs weird HTML’ is closed to new replies.