开发者

WordPress: if image exists, display it

开发者 https://www.devze.com 2023-02-14 01:24 出处:网络
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="<

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

精彩评论

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

关注公众号