I created an unordered list with the following html:
<ul id="infoBox">
<li class="facebook"><a href="<?php echo get_field('facebook_page'); ?>"><?php the_title(); ?> on Facebook</a></li>
<li class="twitter"><a href="<?php echo get_field('twitter_page'); ?>">Follow <?php the_title(); ?> on Twitter</a></li>
<li class="youtube"><a href="<?php echo get_field('youtube_开发者_Python百科page'); ?>">Watch <?php the_title(); ?> on Youtube</a></li>
</ul>
<?php echo get_field('value'); ?>
is grabbing a string from the backend of my site. Sometimes, I do not have a string to display, so I want to create a conditional statement in jquery and/or php that basically says: If there is no field to get (if the field is left empty on the backend), do not display the list item at all. For instance, if a band doesn't have a youtube page, do not display the list item with a class of 'youtube' at all.
Any idea how I would go about this?
<ul id="infoBox">
<?php $response = get_field('facebook_page');
if(!empty($response)): ?><li class="facebook"><a href="<?php echo $response; ?>"><?php the_title(); ?> on Facebook</a></li><?php endif; ?>
<?php $response = get_field('twitter_page');
if(!empty($response)): ?><li class="twitter"><a href="<?php echo $response; ?>">Follow <?php the_title(); ?> on Twitter</a></li><?php endif; ?>
<?php $response = get_field('youtube_page');
if(!empty($response)): ?><li class="youtube"><a href="<?php echo $response; ?>">Watch <?php the_title(); ?> on Youtube</a></li><?php endif; ?>
</ul>
Would this work for you?
if(get_field('value') == '' || is_null(get_field('value'))
echo 'no value';
else
echo 'there is a value';
I hope understand your question and could help.
<ul id='infoBox'>
<?php
$name = the_title();
$potentialItems = array('facebook' => "$name on facebook",
'youtube' => "Watch $name on youtube",
'twitter' =? "Follow $name on twitter");
foreach($potentialItems as $k=>$v)
{
$gf = get_field($k.'_page');
if($gf)
{
echo "<li class='$k'><a href='$gf'>$v</a></li>";
}
}
?>
</ul>
精彩评论