Search code examples
phphtmlwordpressadvanced-custom-fieldsacfpro

Advanced Custom Fields - don't show field if field is left blank with PHP


i'm using ACF and the repeater to spit out a series of subfields including: image header description button title button link

But, while most of the sliders have all of them, SOME don't have the button title or link so if i don't fill it out, it just shows the blank button with no text.

Can someone assist where in the code below i can hide the button if the user does not add in anything into the field in the meta field in the WP backend?

<?php if( have_rows('feature_slider') ): ?>
    <div class="swiper-container">
        <div class="swiper-wrapper">

    <?php while( have_rows('feature_slider') ): the_row(); 
        $feature_image = get_sub_field('feature_image');
        $card_heading = get_sub_field('card_heading');
        $card_description = get_sub_field('card_description');
        $card_button_text = get_sub_field('card_button_text');
        $card_link_url = get_sub_field('card_link_url');

     ?>


            <!-- slider -->
            <div class="swiper-slide"> 
            <div class="bg--pattern"></div>  
                <div class="absolute c-card"><div class="card-content">
                    <h2><?php the_sub_field('card_heading'); ?></h2>
                     <p><?php the_sub_field('card_description'); ?></p>
                    <a class="card-btn" href="<?php the_sub_field('card_link_url'); ?>"><?php the_sub_field('card_button_text'); ?> &xrarr;</a></div></div>
                
                    <img src="<?php echo esc_url($feature_image['url']); ?>" alt="<?php echo esc_attr($feature_image['alt']); ?>" />
                    
                 <div class="c--content-card">
                     <h2><?php the_sub_field('card_heading'); ?></h2>
                     <p><?php the_sub_field('card_description'); ?></p>
                 </div>  <!-- card -->
                 
             </div> <!-- swiper slide -->


    <?php endwhile; ?>
<?php endif; ?>

The button you'll see that i'm trying to hide is begins <a class="card-btn" .... but would also be useful to know how to do it for other ACF php I add in.

Thank you!


Solution

  • A simple !empty() check will do it.

    $btn_text = get_sub_field('card_button_text');
    
    if (!empty($btn_text)) {
      $card_link = get_sub_field('card_link_url');
      echo '<a class="card-btn" href="' . esc_url($card_link) . '">' . esc_html($btn_text) . '</a>';
    }
    

    Update: Full code as @Churchill requested:

    <?php if ( have_rows('feature_slider') ) : ?>
      <div class="swiper-container">
        <div class="swiper-wrapper">
    
            <?php while ( have_rows('feature_slider') ) : 
              the_row(); 
              
              $feature_image = get_sub_field('feature_image');
              $card_heading = get_sub_field('card_heading');
              $card_description = get_sub_field('card_description');
              $card_button_text = get_sub_field('card_button_text');
              $card_link_url = get_sub_field('card_link_url'); ?>
    
              <!-- slider -->
              <div class="swiper-slide"> 
                <div class="bg--pattern"></div>  
                <div class="absolute c-card">
                  <div class="card-content">
                    <h2><?php the_sub_field('card_heading'); ?></h2>
                    <p><?php the_sub_field('card_description'); ?></p>
                    <?php
                      if (!empty($card_button_text)) {
                        echo '<a class="card-btn" href="' . esc_url($card_link_url) . '">' . esc_html($card_button_text) . ' &xrarr;</a>';
                      }
                    ?>
                  </div>
                </div>
                
                <img src="<?php echo esc_url($feature_image['url']); ?>" alt="<?php echo esc_attr($feature_image['alt']); ?>" />
                    
                <div class="c--content-card">
                  <h2><?php the_sub_field('card_heading'); ?></h2>
                  <p><?php the_sub_field('card_description'); ?></p>
                </div>  <!-- card -->
              </div><!-- swiper slide -->
    
            <?php endwhile; ?>
    
          </div><!-- swiper-wrapper -->
        </div><!-- swiper-container -->
    <?php endif; ?>