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

时间:2015-10-29 19:31:25

标签: php wordpress if-statement

当图像字段为空时,我在更改默认图像时遇到一些问题。这里有两个不同的图像。我想组合它们,如果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"/>

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

<?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
    }
?>