conditional statement if all is empty, don't show
<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 } 开发者_如何学Python?>
</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?
I would suggest changing your design a little bit.
//Initialize the variables and define a fixed array for easy manipulation
if($website) { $artist['website'] = $website; }
if($youtube) { $artist['youtube'] = $youtube; }
if($twitter) { $artist['twitter'] = $twitter; }
// If somehow, $artist contains values
if(count($artist)) { ?>
<ul class="artist-links"
<? foreach($artist as $key=>$value) { ?>
<li class="artist-<?=$key?>">
<a href="<?=$value?>" target="_blank">
<img src="theimage.jpg" />
</a>
</li>
<? } ?>
<?
}
?>
Just check at the beginning if they are all empty:
Code
<?php if(!(empty($artist_website) && empty($artist_youtube) && empty($artist_twitter))) : ?>
<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>
<?php endif; ?>
Explanation
Basically we check each variable for the condition "empty". If all 3 are empty, the result of AND(true,true,true)
is true
. Then we see the "!" or the NOT operator, which reverses that true
and makes it a false
. 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.
If you want to do exactly what you said above:
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.
Do this
<?php
if(!empty($artist_website) || !empty($artist_youtube) || !empty($artist_twitter)) {
//Do what you want to do. Like show the ordered list.
} //End of if statement
?>
If at least one of the variables has a non empty value the conditional is passed.
精彩评论