简体   繁体   中英

Wordpress Facebook Share Thumbnail (PHP)

I'm aware that when people share pages from your site on Facebook it uses the "image_src" tag to find a preview image. I want Facebook to look for a post thumbnail and use that first, and if that's not present, to use a default.

Can someone just verify that this code should work? I think Facebook isn't caching/catching up right now, but I just want a nod from someone more advanced to make sure I'm not making a fundamental mistake.

<link rel="image_src" href="
<?php
if ( has_post_thumbnail() ) {
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );
echo $thumbnail_src;
}
else echo "/link.jpg"; ?>
" /> 

Why don't you use Facebook's Open Graph META to define the image (for share) and as long as your thumbnail is set as the Featured image the below code ( found here ) should work.

<!-- facebook open graph stuff -->
<meta property="fb:app_id" content="xxxxxxxxx"/>
<meta property="og:site_name" content="MySite"/>
<meta property="og:url" content="http://www.mysite.com"/>
<meta property="og:title" content="Story Title" />
<meta property="og:image" content="
<?php
    $thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ))
    if($thumbnail_src){ 
    echo $thumbnail_src;
    } else { 
    echo "/link.jpg"; } ?> 
"/>
<!-- end facebook open graph -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM