Support

Account

Home Forums Add-ons Flexible Content Field alternating blocks with customizable number of columns can't be achieved easily

Solved

alternating blocks with customizable number of columns can't be achieved easily

  • the scenario I’m trying to achieve aims to give the user the possibility to freely alternate blocks the have different number of columns.

    So I’ve created a flexible content the same way the video of the documentation shows, where a select field determines how many WYSIWYG editors should appear (when the condition matches the value of the selection).

    Now, suppose I’d like to get a structure like this:

    a 1 column block followed by
    a 3 column block followed by
    a 2 column block followed and so on…

    The smartest way should be that every time I want to create a new block I have to add in the editor the same flexible content, again and again as my layout requires, but doing this way the loop code always calls the 3rd colum subfields even where I’ve defined I want a 1 column block… that’s why the loop always calls the same slug because it finds it compiled even where it wasn’t.

    Is there a way to workaround this? Any help will be appreciate!

  • I’m not sure what this means

    that’s why the loop always calls the same slug because it finds it compiled even where it wasn’t.

    If you share the code that’s giving you a problem it would be helpful to understand the issue.

  • Hi, code is the following

    <?php
    if( have_rows(‘flexy’) ):
    while ( have_rows(‘flexy’) ) : the_row();
    if( get_row_layout() == ‘colonne’ ):
    the_sub_field(‘numero_colonne’);
    the_sub_field(‘colonna_1’);
    the_sub_field(‘colonna_2’);
    the_sub_field(‘colonna_3’);
    endif;
    endwhile;
    endif;
    ?>

    Imagine using many times in the same website page the flexible content “text” of ACF demo video, cause you want to switch many times from a 1 column layout to a 2 (or more) column layout.

    Hope it helps! thank you 🙂

  • It you have a select or radio field that determines the number of columns then you need to test that field.

    
    if (have_rows('flexy')) {
      while (have_rows('flexy')) {
        the_row();
        if (get_row_layout() == 'colonne') {
          $columns = get_sub_field('numero_colonne');
          // always show column 1
          the_sub_field('colonna_1');
          // show other content based on columns selected
          if ($columns > 1) {
            the_sub_field('colonna_2');
          }
          if ($columns > 2) {
            the_sub_field('colonna_3');
          }
        }
      }
    }
    
  • BINGO… you made it man, it works like a charm!

    Thank you very much, I’m really happy with this! 🙂

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

The topic ‘alternating blocks with customizable number of columns can't be achieved easily’ is closed to new replies.