如何在ACF中添加if else

How to add if else in ACF

<?php
$featured_posts = get_field('director');
if( $featured_posts ): ?>
    <ul>
    <?php foreach( $featured_posts as $featured_post ):?>
      
        <div class="movies-series-director">
                <a class="movies-series-director-thumbnails" href="<?php echo get_page_link($featured_post->ID);?>"> <img src="<?php echo get_the_post_thumbnail_url($featured_post->ID, 'thumbnail');?>"></a>
            <div class="movies-series-director-title">
                <h4>  <a href="<?php echo get_page_link($featured_post->ID);?>"> <?php echo $featured_post->post_title;?> </a> </h4> 
                <b>Director</b>
            </div>               
        </div>
       
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

大佬们怎么输出啊

如果有缩略图则post缩略图 否则,如果没有缩略图,则打印这张图片(无图片)

这是您的工作代码:


<?php
$featured_posts = get_field('director');
if( $featured_posts ): ?>
    <ul>
    <?php foreach( $featured_posts as $featured_post ):?>
      
        <div class="movies-series-director">
                <a class="movies-series-director-thumbnails" href="<?php echo get_page_link($featured_post->ID);?>"> <img src="<?php echo get_the_post_thumbnail_url($featured_post->ID, 'thumbnail');?>"></a>
            <div class="movies-series-director-title">
                <h4>  <a href="<?php echo get_page_link($featured_post->ID);?>"> <?php echo $featured_post->post_title;?> </a> </h4> 
                <b>Director</b>
            </div>               
        </div>
       
    <?php endforeach; ?>
    </ul>
<?php else: ?>
    .... here your code for "else"
<?php endif; ?>

您想检查缩略图是否为空,如果没有缩略图,则显示默认图像。

所以你使用get_the_post_thumbnail_url()并检查它是否为空。

if ( empty(get_the_post_thumbnail_url( $featured_post->ID)) ):
    echo 'https://your-default-image_url.jpg';
else:
    echo get_the_post_thumbnail_url( $featured_post->ID, 'thumbnail' );
endif;

如果您希望它更具可读性,您的代码可以如下所示:

<ul>
<?php foreach( $featured_posts as $featured_post ):?>
    <?php $featured_image = get_the_post_thumbnail_url($featured_post->ID, 'thumbnail') ?: 'https://your-default-image_url.jpg'; ?>
  
    <div class="movies-series-director">
            <a class="movies-series-director-thumbnails" href="<?php echo get_page_link($featured_post->ID);?>"> <img src="<?php echo $featured_image;?>"></a>
        <div class="movies-series-director-title">
            <h4>  <a href="<?php echo get_page_link($featured_post->ID);?>"> <?php echo $featured_post->post_title;?> </a> </h4> 
            <b>Director</b>
        </div>               
    </div>
   
<?php endforeach; ?>
</ul>