显示自定义字段(如果有内容)或隐藏(如果没有)

4 php wordpress jquery

我正在显示这样的自定义字段:

<div class="blog_text">
    <img src="<?php the_field('imagen_prensa'); ?>" />
</div>
Run Code Online (Sandbox Code Playgroud)

我想只在用户上传图像时才显示img,因为现在如果没有图像,则会出现损坏的图像符号.

我尝试了以下,但它不起作用:

<?php if (get_field('imagen_prensa') != ''): ?>
     <img src="<?php the_field('imagen_prensa'); ?>" />
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

任何可以实现的吗?

编辑:

这是现在使用下面的答案的代码,但现在它只给出一个空白页面:

<div id="blog_interior">

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <article>
            <div class="article_date"><?php the_time('d/m/Y'); ?></div>
            <h2><?php echo get_the_title(); ?></h2>
            <div class="blog_text">
                <?php if ( !empty(the_field('imagen_prensa')) ): ?>
                    <img src="<?php echo the_field('imagen_prensa'); ?>" />
                <?php endif; ?>
                <div class="video_prensa"><?php the_field('video_prensa'); ?></div>
                <?php the_content() ?>
            </div>
        </article>

        <?php endwhile; ?>    
            <div class="back">

            </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

Gho*_*man 6

empty empty - 确定变量是否为空

if (!empty(get_field('imagen_prensa'))) {
    // Display image
}
Run Code Online (Sandbox Code Playgroud)