How to Fix The Thumbnail When Sharing your Posts on social media | Using 4 Methods

How to Fix The Thumbnail When Sharing your Posts on social media | Using 4 Methods

Tech support

4 года назад

3,686 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@peterjigstad9755
@peterjigstad9755 - 03.07.2022 06:17

The method 4 works best. But there's some wrong characters in your above code, down here is a corrected one that works, I've also change the thumbnail to Full instead of medium:
Remember that after you have saved the functions.php, also update homepage with your post and then do a "Scrape Again" on facebook's sharing debugger page.

function insert_og_image() {
global $post;
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image= get_template_directory_uri()."assets/images/defaultshareimage.jpg"; //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
}
add_action( 'wp_head', 'insert_og_image', 5 );

Ответить