doulu1914 2013-10-01 04:33
浏览 31

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

I have this code inserted in the Function.PHP file of my wordpress this. What it basically do is:

when ever the user clicks the pinterest button, it ignores all the images in the blog post page but instead choose/return the feature image to be pinned.

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

I have no problem with the code above, but then I realized that what if there's no featured image?

Is it possible if someone can modify the code for to to add the following IF and Else Condition:

IF Featured Image is Present:

Run the script above. (I think it's covered in the above code and works fine)

BUT IF THERE'S NOT FEATURED IMAGE, choose/return a default image to be pinned.

I'm not really sure how to integrate this code (below) to above code. Sorry but i lack of knowledge in this area.

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

THank you verymuch

  • 写回答

1条回答 默认 最新

  • douling8772 2013-10-01 04:36
    关注

    Use the following code:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度