Smart Excerpts

archive-layouts

Added Feature

Twenty Fifteen doesn’t have any options for displaying your posts with excerpts at all. With Matt2015 if you are using the “Fancy Rollver” Archive layout, or the Excerpt layout you have Smart Excerpts automatically.

Where to Find It?

This feature doesn’t have any options.

This feature ensures that every post in your Archives has a well formatted and consistent length excerpt whether you created one for the post or not. Here’s how it does it:

  1. If Yoast SEO is active and there is a meta description for the post, show that as the excerpt.
  2. If not, check for an excerpt and show that.
  3. Lastly, if there is neither a Yoast SEO meta description, nor an excerpt provided, trim the content of the first words of the post.

This logic prioritizes your best excerpts (meaning the ones you created intentionally) first. Obviously, if you are using Yoast’s SEO for WordPress you probably spent time thinking of a quality excerpt for your Google search results. If you did that for a post, we might as well use that for your users as well.

But some prefer to craft their excerpts manually with the WordPress default Excerpt field. That’s next in line.

Lastly, if you just didn’t put the time into creating an excerpt, Matt2015 will take the first lines of your post, strip the shortcodes and media out of it and display the text only up to the word limit that you set. That’s right, there is also a setting for setting the number of words to display in the excerpt which applies to all three of those types of excerpt.

archive-layouts

The Code

If you are a developer, then you’ll appreciate this:

$yoastdesc = get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true);
$excerptlength = get_theme_mod('excerpt_length', 25);
$excerpt = get_the_excerpt();
$content = get_the_content();
$screenreader = '<a href="' . get_permalink() . '"><span class="screen-reader-text">' . get_the_title( ) . '</span>Read More &hellip;</a>';
 
if(!empty($yoastdesc)) {
    $trimyoast = wp_trim_words($yoastdesc, $excerptlength, $screenreader);
    echo $trimyoast;
} 
elseif(has_excerpt() == true) {
    $trimexcerpt = wp_trim_words( $excerpt , $excerptlength, $screenreader );
    echo strip_shortcodes($trimexcerpt);
} 
else {
    $trimmed_content = wp_trim_words( $content, $excerptlength, $screenreader );
    echo strip_shortcodes($trimmed_content);
}