在 PHP 脚本中的 IF 中添加一个 ELSE 以获取默认的 Image


Add an ELSE to the IF in PHP script to get the default Image

我在我的wordpressthis的Function.PHP文件中插入了这段代码。它的基本作用是:

当用户点击 Pinterest 按钮时,它会忽略博客文章页面中的所有图像,而是选择/返回要固定的功能图像。

function catch_that_image( $size = 'full' ) {
    global $post;
    if ( has_post_thumbnail($post->ID) ) {
        $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
        return $featured_image[0];
    }
    return false;
}

我对上面的代码没有问题,但后来我意识到如果没有特色图片怎么办

如果有人可以修改 to 的代码以添加以下 IF 和 Else 条件,是否可以:

如果存在特色图像:

运行上面的脚本。(我认为它包含在上面的代码中并且工作正常(

但是,如果没有精选图像,请选择/返回要固定的默认图像。

我不太确定如何将这段代码(如下(集成到上面的代码中。对不起,但我缺乏这方面的知识。

if(empty($first_img)){ //Defines a default image
    $first_img = "http://www.mywebsite/wp-content/themes/Default_Image.jpg";
  }

非常感谢你

使用以下代码:

function catch_that_image( $size = 'full' ) {
   global $post;
   if ( has_post_thumbnail($post->ID) ) {
       $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
       if(empty($featured_image[0])){ //Defines a default image
         $featured_image[0] = "http://www.mywebsite/wp-content/themes/Default_Image.jpg";
       }
       return $featured_image[0];
   }
   return false;
}