如果字段为空,则隐藏php链接


Hide php link if the field is empty?

我试图显示一个链接,只有当它有一个值。

如果the_field imdb不是空的,我怎么能得到图像?

<a href="<?php the_field('imdb'); ?>" >
  <img style="width:60px;"src="/img/link.png" /></a>

get_field();代替the_filed();

if(!empty(get_field("fildname"))){
#your code hear
}

尝试:-

if(!empty(the_field('imdb'))){
    <a href="<?php the_field('imdb'); ?>" ><img style="width:60px;"src="/img/link.png" /></a>
}

使用isset

if(isset(the_field('imdb'))&&!empty(the_field('imdb'))){
<img src=""/>
}

因为我不想在旧帖子上显示img或链接,所以我解决了这个问题:

<?php 
// assign image
$imdbimg = "<img src='http://mydomain.com/IMDb.png' class='imdb' />";
// imdb link from custom field
$imdblink = get_field('imdb');
?>
// display img and link on post newer then id 16
<?php global $post;
if ( $post->ID >= 16 ){  
echo '<a href="' . $imdblink . '" target="_blank"> ' . $imdbimg . '</a>';
}    
?>