开发者

WordPress: if image exists, display it

I'm using this to display a banner image in my WordPress site:

<img src="../../wp-content/uploads/<?php echo get_post_meta($post->ID, 'image-banner', true); ?>" alt="<?php the_title(); ?> banner" />

However, there will be some pages with no banner image.

How 开发者_Go百科can I rework this check if the image (or 'image-banner' field) exists before displaying the img tag?

Thanks in advance!


I think it's better you use a variable to store img url, so you could just check if it is empty.. Like this:

<?php
$imgBanner = get_post_meta($post->ID, 'image-banner', true);
if (!empty($imgBanner)) {
?>
<img src="../../wp-content/uploads/<?php echo $imgBanner; ?>" alt="<?php the_title(); ?> banner" />
<?php
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜