Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

OK, I know there are many questions and articles about displaying post thumbnail (i.e. featured image) as a thumbnail for Facebook sharing but I'm still having trouble with it.

I'm using AddThis plugin for Wordpress and have a toolbox that shows FB share, Twitter share, G+, Digg, StumbleUpon. When I want to share through Facebook, the title and excerpt show correctly but the thumbnail image is not my featured image but the first image from the body of the post. I can choose an image to use as thumbnail inside a share window and that way use featured image as a thumbnail but I want the featured image to be the first one displayed without the option to change it to any other image from the post.

I tried everything I could think of - plugins that add Open Graph Tags, I tried adding OG Tags manually to head, copy/pasted the tags from four or five different articles, used clear OG Tags and used them inside if statements; I don't know what else to try.

Could you please tell me the exact code that I should put inside my head tags so that when I share through Facebook, there is no option for choosing a thumbnail but that it always shows a thumbnail of a post.

Also, I know that FB caches the images when a share request is sent so I used a new post each time I tested the code.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Put this in your head

<?php $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id( get_the_ID() ), 'thumnail'); ?>
<?php if ($fb_image) : ?>
    <meta property="og:image" content="<?php echo $fb_image[0]; ?>" />
<?php endif; ?>

To test it out, you don't have to create a new post, just paste your URL in here: https://developers.facebook.com/tools/debug Here you can the data and possibly some errors.

share|improve this answer
    
Yeah, this debugger is great and it does show my featured image as a thumbnail but still if I try and share through my site, the first thumbnail displayed is the first image from the post. I guess I can't stop other images from the post being available for choosing, can I? –  John Doe Dec 20 '11 at 9:38
    
Strainge. Do you have a link to your site so we can check for you? –  Rob Vermeer Dec 20 '11 at 13:51
    
Here, I can create a new post if you want, with the same thumbnail and pics inside a post and you'll see that the first image displayed inside post is always the default thumbnail and you can click the arrow until you get to the featured image. –  John Doe Dec 20 '11 at 14:04
1  
If I lke the page it will show the correct (first) thumbnail. When I share the page it will not use the first thumbnail... Have you tested it with only one thumbnail in your head? Now I see 7 time og:image in your source. –  Rob Vermeer Dec 20 '11 at 14:30
1  
You can disable some plugins and see what happends. –  Rob Vermeer Dec 20 '11 at 15:07
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.