ACF 如果字段一可用则显示字段一否则 wordpress 标题

ACF if field one available show field one otherwise wordpress title

我需要使用 ACF 插件的具有以下值的 if else 语句。

我有一个名为 "headline":

的字段
<?php if ( get_field( 'headline') ) { ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark">
        <img src="<?php the_field( 'headline' ); ?>" />
    </a>
<?php } ?>

Wordpress 标题代码:

<a href="<?php echo esc_url(get_permalink()); ?>">
     <?php echo get_the_post_thumbnail(get_the_ID(), $thumbnail_size , array('alt' => get_the_title(),'title' => ''));  ?>
</a>

我想要的:如果字段一标题可用则显示字段一否则显示 wordpress 标题的第二个代码

试试这个代码:

<?php if(!empty(get_field( 'headline'))) { ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark">
    <img src="<?php the_field( 'headline' ); ?>" />

<?php } else { ?>
    <a href="<?php echo esc_url(get_permalink()); ?>">
    <?php echo get_the_post_thumbnail(get_the_ID(), $thumbnail_size , array('alt' => get_the_title(),'title' => ''));  ?></a>

<?php } ?>
<?php $headline = get_field( 'headline'); ?>
<?php if($headline) { ?>
     <a href="<?php the_permalink(); ?>" rel="bookmark">
         <img src="<?php the_field( 'headline' ); ?>" />
     </a>
<?php } else { ?>
     <a href="<?php echo esc_url(get_permalink()); ?>">
         <?php echo get_the_post_thumbnail(get_the_ID(), $thumbnail_size , array('alt' => get_the_title(),'title' => ''));  ?>
     </a>
<?php } ?>