【发布时间】:2014-09-05 11:46:36
【问题描述】:
我试图仅在链接具有值时才显示它。
如果the_field imdb不为空,如何获取图片?
<a href="<?php the_field('imdb'); ?>" >
<img style="width:60px;"src="/img/link.png" /></a>
【问题讨论】:
我试图仅在链接具有值时才显示它。
如果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=""/>
}
【讨论】:
isset() 来自empty() 文档empty is essentially the concise equivalent to !isset($var) || $var == false。
由于我不想在旧帖子上显示 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>';
}
?>
【讨论】: