1

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!

Churchill
  • 19
  • 5
  • wrap it in standard PHP `IF` checking for a return value from `the_sub_field('card_link_url')` and `the_sub_field('card_button_text')` ? – Scuzzy Aug 15 '22 at 04:47
  • 2
    sorry, that should be `get_sub_field()`, `the_sub_field` is a wrapper function for `get_sub_field` with an echo statement. https://www.advancedcustomfields.com/resources/get_sub_field/ – Scuzzy Aug 15 '22 at 04:53

1 Answers1

0

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; ?>
Muhammad Zohaib
  • 307
  • 1
  • 5
  • 15
  • You should use [`wp_kses_post()`](https://developer.wordpress.org/reference/functions/wp_kses_post/) instead of just echo even when escaping strings. – odil-io Aug 15 '22 at 07:42
  • can you please explain and give a reason why we should use it for hardcode? Because I've seen many themes even the `Twenty Twenty-Two` is not using `wp_kses_post()` for hardcoded elements. – Muhammad Zohaib Aug 15 '22 at 14:39
  • @MuhammadZohaib Thanks for the response - not sure i 100% follow. Are you able to copy the code i had put above and adjust / add your code where it needs to go? I tried to add it but it made all the buttons disappear - even the ones that have text/url in the WP Dash – Churchill Aug 16 '22 at 01:42
  • 1
    @MuhammadZohaib sure I can, according to the [Plugin Handbook](https://developer.wordpress.org/plugins/security/securing-output/) 'All output must be run through an escaping function'. Having said that, Personally, I find it less readable if you switch up the method of outputting HTML+PHP. I would rewrite those lines with an if like so: '' then out put your html using the `esc_`-functions you already do and then close with ``. But thats me. – odil-io Aug 16 '22 at 07:04
  • @OdilioWitteveen I do understand what is mentioned on that plugin handbook, but isn't that for dynamic values/elements and translation strings? escaping a hardcoded element seems redundant to me. Its not like its gonna change to a dynamic value by its self. Please correct me if I am wrong. – Muhammad Zohaib Aug 16 '22 at 14:40
  • @Churchill If it works then kindly mark it as accepted answer. That's how appreciation works in StackOverflow ;-) – Muhammad Zohaib Aug 16 '22 at 19:32
  • 1
    @MuhammadZohaib No worries, you're not wrong! The Plugin Handbook defines how one _should_ code their plugins/themes. It isn't specific to dynamic values but I understand your approach. I agree, since the output is known, nothing is factually wrong here. – odil-io Aug 17 '22 at 07:22
  • Also, this isn't completely your code. (I honestly forgot). – odil-io Aug 17 '22 at 07:26