<ul class="artist-links">
<?php if(!empty($artist_website)) { ?><li class="artist-website"><a href="<?=$artist_website?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-website.png" alt="<?php the_title(); ?> Website" /></a></li><?php } ?>
<?php if(!empty($artist_youtube)) { ?><li class="artist-youtube"><a href="http://youtube.com/<?=$artist_youtube?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-youtube.png" alt="<?php the_title(); ?> Youtube" /></a></li><?php } ?>
<?php if(!empty($artist_twitter)) { ?><li class="artist-twitter"><a href="http://twitter.com/<?=$artist_twitter?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-twitter.png" alt="<?php the_title(); ?> Twitter" /></a></li><?php } ?>
</ul>
I want to make this unordered list show up only if at least one of the variables (artist-website, artist-youtube, artist-twitter) isn’t empty. So if all of the variables are empty, then this list won’t show up at all. What can I add before this code to make this work?
Just check at the beginning if they are all empty:
Code
Explanation
Basically we check each variable for the condition “empty”. If all 3 are empty, the result of
AND(true,true,true)istrue. Then we see the “!” or the NOT operator, which reverses thattrueand makes it afalse. So if all the variables are empty, then do no print the lines in between.You may also notice the “:” / “endif” syntax i’ve used, which I find much neater when splicing into tags of html.