wordpress中的If/else语句将上传的图像更改为默认值


If / else statement in wordpress to change uploaded image to default

当图像字段为空时,我在更改默认图像时遇到了一些问题。这是两个不同的图像。我想组合它们,如果get_field('author_photo')为空,那么上传第二个。

  <?php 
       $image = get_field('author_photo');
       if( !empty($image) ): 
  ?>
  <img src="<?php echo $image['url']; ?>" height="80" id="img-sp" alt="<?php echo $image['alt']; ?>" />
  <?php endif; ?>              
  <? if (get_user_featured_image_id('mentor','user_'.get_the_author_ID())){ ?>
  <?php $image = p_get_attachment_image_src(get_field('user_featured_image', 'user_'.get_the_author_ID()),'small-profile-thumbnail'); 
  ?>
  <img src="<?php echo @$image[0]; ?>" height="80"  alt="" id="img-sp"/>

提前感谢!

昨天解决了它。谢谢你的回答。

<?php
   $image = get_field('author_photo');
    $src_link = "";
    $alt_text = "";
    if (!empty($image)) {
        $src_link = $image['url'];
        $alt_text = $image['alt'];
    } elseif (get_user_featured_image_id('mentor','user_'.get_the_author_ID())) {
        $image = wp_get_attachment_image_src(get_user_featured_image_id('mentor','user_'.get_the_author_ID()),'small-profile-thumbnail');
        $src_link = @$image[0];
    }
    if ($src_link) { ?>
        <img src="<?php echo $src_link; ?>" height="80" id="img-sp" alt="<?php echo $alt_text; ?>" /><?php
    }
?>