How to show post's excerpt in Facebook description

Posted in: Newspaper
Post count: 15

Now, the post’s description in facebook is the general description.

I would like it to be the excerpt.

Post count: 9544

Add proper open graph (OG) tags which FB needs to do that.

Post count: 15

How? I added this code in funtions.php but does not work.

function fb_opengraph() {
global $post;

if(is_single()) {
if(has_post_thumbnail($post->ID)) {
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
} else {
$img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
}
if($excerpt = $post->post_excerpt) {
$excerpt = strip_tags($post->post_excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
?>

<meta property="og:description" content="<?php echo $excerpt; ?>"/>

<?php
} else {
return;
}
}
add_action('wp_head', 'fb_opengraph', 5);

Post count: 22421

Hello,
Please follow this guide: https://forum.tagdiv.com/facebook-share/
Thank you!

Post count: 15

Thanks, but I would like to work without plugins

Post count: 22421

Hello,
If you do not want to use plugin, it means you want a custom OG tag implementation. In this case you can follow a few resources online on the subject:
https://www.elegantthemes.com/blog/tips-tricks/how-to-add-open-graph-tags-to-wordpress
This will require coding knowledge though.
(plugins do not add OG tags in our theme files but in the wp files…so that’s your best option to alter the wp files or create your own plugin)
Thank you!

Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic.