Leaky Eddie
Leaky Eddie

Reputation: 117

if / else in wordpress to pull a specific image

I had some trouble with facebook pulling the wrong images from Wordpress links added to the FB news feed. I was able to correct it with the "if" snippet of code below. I added the "else" statement to provide for a fall-back image in case the user hasn't added a featured image to the linked WP post, but it doesn't seem to be working. Obviously I'm no php coder... any help?

<?php
if ( has_post_thumbnail())
 {
 $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
}

else {
 $fb_image = wp_get_attachment_image_src ("http://MY-IMAGE-URL"), 'full');
}

?>

I also tried the code below in the else statement, but to avail

wp_get_attachment_image_src( 732, $size, $icon )

Thanks in advance

Upvotes: 0

Views: 136

Answers (1)

Kris Van den Bergh
Kris Van den Bergh

Reputation: 1152

The function wp_get_attachment_image_src is used to get the URL (src) for an image attachment. However, you cannot directly pass on an URL like you do as it works based on an attachment ID.

In your else statement, replace the line with the following in order to work with a default image :

$fb_image = "http://MY-IMAGE-URL";

Hope this helps

Upvotes: 1

Related Questions