Support

Account

Home Forums Add-ons Flexible Content Field Echo nothing if value is X Reply To: Echo nothing if value is X

  • For the record, I am having a little success with the following…

    <div class="col-md-12 cxt-title <?php
                                        // if "centre" radio selected, use relevant bootstrap 4 classes
                                        if( get_sub_field('jumbotron_horizontal_alignment') == 'Centre' ):
                                        	echo 'd-flex text-center justify-content-center';
                                        elseif( get_sub_field('jumbotron_horizontal_alignment') == 'Left' ):
                                          echo '';
                                        endif;
                                        ?>"><!-- for left align, remove d-flex text-center justify-content-center  -->

    That is – jumbotron_horizontal_alignment is a radio button with possible values “Centre” or “Left”. If “Centre”, echo out the three Bootstrap 4 classes that centre-align the text. If “Left”, echo nothing.

    I am learning a bit more ACF today, winging it a bit.
    (Meanwhile, also learning Bootstrap 4! I’m not certain that left alignment shouldn’t also be controlled using text-left and justify-content-start, but left seems to be the default).

    The above portion forms part of my overall Jumbotron layout…

    <?php
    
    // check if the flexible content field has rows of data
    if( have_rows('page_components') ):
    
         // loop through the rows of data
        while ( have_rows('page_components') ) : the_row();
    
            if( get_row_layout() == 'jumbotron' ): ?>
    
              <!-- jumbotron -->
              <div class="jumbotron jumbotron-fluid mb-0 cxt-bgimg <?php the_sub_field('jumbotron_height');?> cxt-bgimg-tint-default <?php the_sub_field('background_image');?> text-white d-flex justify-content-center">
                <div class="container align-self-center">
                  <div class="row">
                    <div class="col-md-12 cxt-title <?php
                                        // if "centre" radio selected, use relevant bootstrap 4 classes
                                        if( get_sub_field('jumbotron_horizontal_alignment') == 'Centre' ):
                                        	echo 'd-flex text-center justify-content-center';
                                        elseif( get_sub_field('jumbotron_horizontal_alignment') == 'Left' ):
                                          echo '';
                                        endif;
                                        ?>"><!-- for left align, remove d-flex text-center justify-content-center  -->
                      <div class="align-self-center p-3">
                        <h1 class="display-4 pb-2"><?php the_sub_field('jumbotron_title');?></h1>
                        <p class="lead pb-4"><?php the_sub_field('jumbotron_subtitle');?></p>
                        <?php
                        // check if the flexible content field has rows of data
                        if( have_rows('button') ):
                             // loop through the rows of data
                            while ( have_rows('button') ) : the_row();
                                if( get_row_layout() == 'button' ):
                                  echo '<a class="btn btn-primary pmd-ripple-effect btn-lg" href="' . get_sub_field('button_url') .'" role="button">'. get_sub_field('button_title') .'</a>';
                                endif;
                            endwhile;
                        endif;
                        ?>
    
                      </div>
                    </div>
                  </div>
                </div>
              </div>
    
            <?php endif; ?>
    
            <!-- Other layouts snipped from here -->
    
        <?php endwhile;
    
    else :
    
        // no layouts found
        echo 'No ACF layouts';
    
    endif;
    
    ?>