Skip to content

Instantly share code, notes, and snippets.

@mathetos mathetos/smart-excerpts
Last active May 17, 2016

Embed
What would you like to do?
Smart Excerpts
<?php
/*
* This asks first for a Yoast SEO meta description,
* If that's not present, then it asks for the excerpt of the post,
* If that's not present, then it strips the content
* In this case, I have an excerpt length setting in the Customizer
* Both the excerpt and content stripping, also strip shortcodes
* @author Matt Cromwell <reachme@mattcromwell.com>
* @copyright Copyright (c) 2014, Matt Cromwell
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
//Definitions
$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>';
//Check for Yoast meta description first
if(!empty($yoastdesc)) {
//Trim it according the the excerpt length and include Twenty Fifteen's screenreader markup
$trimyoast = wp_trim_words($yoastdesc, $excerptlength, $screenreader);
echo $trimyoast;
//If that fails, check for a manual excerpt
} elseif(has_excerpt() == true) {
//Trim that according to the excerpt length
//and include Twenty Fifteen's screenreader markup
$trimexcerpt = wp_trim_words( $excerpt , $excerptlength, $screenreader );
//There's almost no chance there's shortcodes in there,
//but let's be careful and strip 'em out anyway
echo strip_shortcodes($trimexcerpt);
//If both of those fail, grab the content of the post itself
} else {
//Trim the content according to the excerpt length
//and include Twenty Fifteen's screenreader markup
$trimmed_content = wp_trim_words( $content, $excerptlength, $screenreader );
//Shortcodes are VERY likely here, so strip 'em out
echo strip_shortcodes($trimmed_content);
}
?>
@swissspidy

This comment has been minimized.

Copy link

swissspidy commented Jan 5, 2015

Are you using the get_the_excerpt filter for this? It looks like you're repeating most of the stuff inside core, e.g. trimming and falling back to post content, etc.

IMHO you could just filter get_the_excerpt to add the meta description if available and filter the_excerpt to add the "read more" link.

btw. is the <span class="screen-reader-text"></span> really useful? Couldn't the post title be just inside the title="" attribute?

Don't wanna be nitpicky though; I like the idea of this!

@mathetos

This comment has been minimized.

Copy link
Owner Author

mathetos commented Apr 29, 2015

Hi @swissspidy,

Not sure why I just saw this comment, sorry for the multiple-month-long delay!

The idea is that I want to make sure the BEST excerpt is used whenever possible. In my workflow (and many writers/bloggers I know) they curate the Yoast Meta Description very carefully, which most often means it's the best description of what the post is about. So, if there is a Meta Description, that should appear as the excerpt in my archive page.

This whole script is used in my 2015 child theme, where I've added settings to set the excerpt length to whatever you like, that's why the $excerptlength is there, it pulls that word count from the settings in the Customizer.

Lastly, the screen-reader-text is markup from TwentyFifteen. I haven't tested it fully, but it's intended to make the excerpts more accessible for screenreaders.

Most of this is explained in far better detail in my article I wrote up about this:
https://www.mattcromwell.com/smart-excerpts-in-wordpress-themes/

@mathetos

This comment has been minimized.

Copy link
Owner Author

mathetos commented Apr 29, 2015

I will say additionally that I just today stumbled on the wp_html_excerpt function, which will allow me to replace wp_trim_words completely and will be much cleaner. Just not sure when I'll implement that exactly yet...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.