Support

Account

Home Forums Add-ons Flexible Content Field Flexible content wrong order

Solved

Flexible content wrong order

  • Hello.

    I’m trying to integrate three different layouts in a flexible content field on a page. I have a wysiwyg layout, an image layout and a text field layout.

    But I can’t seem to get it right. After the loop is done I have this:

    1. wysiwyg
    2. wysiwyg
    3. image
    4. text field

    instead of:

    1. wysiwyg
    2. image
    3. wysiwyg
    4. text field

    What can be the issue?
    Thanks!

  • What does your code look like that’s creating the incorrect order?

  • It looks like this:

    <div>
        <?php if( have_rows('post_content') ): ?>
            <?php while ( have_rows('post_content') ) : the_row();
                if( get_row_layout() == 'layout_text' ): ?>
                <?php the_sub_field('text'); ?>
            <?php endif; endwhile; ?>
        <?php endif; ?>
    </div>
    
    <div>
        <?php if( have_rows('post_content') ): ?>
            <?php while ( have_rows('post_content') ) : the_row();
                if( get_row_layout() == 'layout_image' ): ?>
                <?php the_sub_field('image'); ?>
            <?php endif; endwhile; ?>
        <?php endif; ?>
    </div>
    
    <div>
        <?php if( have_rows('post_content') ): ?>
            <?php while ( have_rows('post_content') ) : the_row();
                if( get_row_layout() == 'layout_video' ): ?>
                <div class="videowrapper">
                    <?php the_sub_field('video'); ?>
                </div>
            <?php endif; endwhile; ?>
        <?php endif; ?>
    </div>

    Maybe I should somehow make it in to one loop, but I can’t figure it out.

    Thanks

  • There are in the order that you’re code says they should be, you’re looping through and first showing all the text layouts, then the images and then the videos. If you want to show them in order then all of your if statements need to be inside a single loop.

    
    <?php 
      if (have_rows('post_content')) {
        while (have_rows('post_content')) {
          ?>
            <div>
              <?php 
                the_row();
                $layout = get_row_layout();
                if ($layout == 'layout_text') {
                  // code to show text content
                } elseif ($layout == 'layout_image') {
                  // code to show image content
                } elseif ($layout == 'layout_video') {
                  // code to show video content
                } else {
                  // code to do something else
                }
              ?>
            </div>
          <?php 
        } // end while
      } // end if have_rows
    ?>
    
  • Thanks! Of course that’s the case! 🙂

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

The topic ‘Flexible content wrong order’ is closed to new replies.