How do the websites that are well optimized for sharing and excerpt displays are handling this subject ?
They definitely have separate fields for excerpt / share-text etc. This is the only way to be certain what text gets used.
These sites also make use of special meta
tags in the head
to send Facebook, Twitter et al. information about the content that is being shared.
The plugin you mention, Yoast SEO, is a quick way to achieve this functionality. You could also build something yourself with custom Meta Fields, depending on your needs.
On a bunch of my sites I use this code to replace the_excerpt
with whatever I fill in at Yoast SEO’s meta description field.
/**
* !Replace the_excerpt with Yoast SEO meta-description
*/
if (function_exists('wpseo_init')){
add_filter( 'get_the_excerpt', 'wpse_232590_replace_post_excerpt' );
function wpse_232590_replace_post_excerpt($output)
{
$output=get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true);
return $output;
}
}
So I fill in the Yoast SEO meta description field and that gets used on my site wherever I use the_excerpt
. When you don’t want to fill in special content for Twitter and Facebook meta tags you just leave them empty, the meta-description field will be used. So it’s just one extra field that you would have to fill in.