1

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 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.