Support

Account

Home Forums Add-ons Repeater Field Cleaner way to hide empty fields?

Solved

Cleaner way to hide empty fields?

  • Hey everyone,

    So I’ve gotten my fields formatted mostly the way I like them.

    However, I’ve noticed that my method seems a little “bloated” as far as making the fields conditional so they only show if they have something in them.

    As you can see below I’ve created an if statement for every field in the loop, but I’m wondering if there’s a “cleaner” way to do this?

    Thanks!

    <?php
                    if( have_rows('new_music_submissions') ):
                        while ( have_rows('new_music_submissions') ) : the_row();
                            echo '<h3>'; the_sub_field('band_name'); echo '</h3>';
                            echo '<img class="band-logo" src="'; the_sub_field('band_logoimage'); echo '">';
                            the_sub_field('band_blurb');
                            the_sub_field('band_media_clip');
                            echo '<div class="band-links">';
                            if (get_sub_field('band_official_site')):
                                echo '<a href="'; the_sub_field('band_official_site'); echo '"><img src="' . get_template_directory_uri(). '/icons/firefox.png"></a>';
                            endif;
                            if (get_sub_field('band_facebook')):
                                echo '<a href="'; the_sub_field('band_facebook'); echo '"><img src="' . get_template_directory_uri(). '/icons/facebook.png"></a>';
                            endif;
                            if (get_sub_field('band_twitter')):
                                echo '<a href="'; the_sub_field('band_twitter'); echo '"><img src="' . get_template_directory_uri(). '/icons/TED.png"></a>';
                            endif;
                            if (get_sub_field('band_youtube')):
                                echo '<a href="'; the_sub_field('band_youtube'); echo '"><img src="' . get_template_directory_uri(). '/icons/youtube.png"></a>';
                            endif;
                            if (get_sub_field('band_googleplus')):
                                echo '<a href="'; the_sub_field('band_googleplus'); echo '"><img src="' . get_template_directory_uri(). '/icons/googlep.png"></a>';
                            endif;
                            if (get_sub_field('band_itunes')):
                                echo '<a href="'; the_sub_field('band_itunes'); echo '"><img src="' . get_template_directory_uri(). '/icons/linkedin.png"></a>';
                            endif;
                            if (get_sub_field('band_spotify')):
                                echo '<a href="'; the_sub_field('band_spotify'); echo '"><img src="' . get_template_directory_uri(). '/icons/spotify.png"></a>';
                            endif;
                            if (get_sub_field('band_soundcloud')):
                                echo '<a href="'; the_sub_field('band_soundcloud'); echo '"><img src="' . get_template_directory_uri(). '/icons/soundcloud.png"></a>';
                            endif;
                            if (get_sub_field('band_bandcamp')):
                                echo '<a href="'; the_sub_field('band_bandcamp'); echo '"><img src="' . get_template_directory_uri(). '/icons/VLC.png"></a>';
                            endif;
                            if (get_sub_field('band_reverbnation')):
                                echo '<a href="'; the_sub_field('band_reverbnation'); echo '"><img src="' . get_template_directory_uri(). '/icons/steam.png"></a>';
                            endif;
                            if (get_sub_field('band_myspace')):
                                echo '<a href="'; the_sub_field('band_myspace'); echo '"><img src="' . get_template_directory_uri(). '/icons/myspace.png"></a>';
                            endif;
                            if (get_sub_field('band_amazon')):
                                echo '<a href="'; the_sub_field('band_amazon'); echo '"><img src="' . get_template_directory_uri(). '/icons/adobe.png"></a>';
                            endif;
                            if (get_sub_field('band_instagram')):
                                echo '<a href="'; the_sub_field('band_instagram'); echo '"><img src="' . get_template_directory_uri(). '/icons/instagram.png"></a>';
                            endif;
                            if (get_sub_field('band_lastfm')):
                                echo '<a href="'; the_sub_field('band_lastfm'); echo '"><img src="' . get_template_directory_uri(). '/icons/lastfm.png"></a>';
                            endif;
                            echo '</div>';
                        endwhile;
                        else : echo "No bands found";
                    endif;
    ?>
  • cleaner is in the eye of the beholder. Shorter with less IFs

    
    <?php 
    
      if (have_rows('new_music_submissions')) {
        while (have_rows('new_music_submissions')) {
          the_row();
          echo '<h3>'; the_sub_field('band_name'); echo '</h3>';
          echo '<img class="band-logo" src="'; the_sub_field('band_logoimage'); echo '">';
            the_sub_field('band_blurb');
            the_sub_field('band_media_clip');
            echo '<div class="band-links">';
            $subfields = array(
              'band_official_site' => '/icons/firefox.png',
              'band_facebook' => '/icons/facebook.png',
              'band_twitter' => '/icons/TED.png',
              'band_youtube' => '/icons/youtube.png',
              'band_googleplus' => '/icons/googlep.png',
              'band_itunes' => '/icons/linkedin.png',
              'band_spotify' => '/icons/spotify.png',
              'band_soundcloud' => '/icons/soundcloud.png',
              'band_bandcamp' => '/icons/VLC.png',
              'band_reverbnation' => '/icons/steam.png',
              'band_myspace' => '/icons/myspace.png',
              'band_amazon' => '/icons/adobe.png',
              'band_instagram' => '/icons/instagram.png',
              'band_lastfm' => '/icons/lastfm.png'
            ); // end array
            foreach ($subfields as $field => $image) {
              $value = get_sub_field($field);
              if ($value) {
                echo '<a href="'.$value.'"><img src="'.get_template_directory_uri().$image.'"></a>';
              }
            } // end foreach field
            echo '</div>';
        } // end while
      } else {
        echo "No bands found";
      }
    ?>
    
  • Thanks John, that’s pretty much exactly what I was trying to do, didn’t realize I needed to make a whole new array 🙂

  • Well, there are probably other ways do to that. I have a tendency to use arrays so that I can use them in loops like the one above, especially when dealing with that many fields.

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

The topic ‘Cleaner way to hide empty fields?’ is closed to new replies.