Home › Forums › Add-ons › Repeater Field › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.