Always Show Excerpts
A WordPress plugin that lets you show the Post excerpt instead of the full content.
Intro
While developing the Keep Pagination in Same Taxonomy plugin, I discovered that the default 2017 WP theme showed the full post in its indexes. So, I wrote this plugin to force it to show excerpts instead so I could see what I was doing in the first place…
Along with my usual plugin code (initialisation and configuration from the “Reading” settings page), we put our hook late on into the_content
call so we can check that we really want to be displaying the full post content.
add_filter('the_content', array($this, 'excerpt_not_content'), 99);
excerpt_not_content()
Rather than simply checking is_archive()
- which misses out the home page and search results anyway - we opt for a finer grained control over which particular types of archive indexes will show excerpts.
public function excerpt_not_content($output) {
$excerpt = false;
foreach ($this->settings as $type => $active) {
if ($active != false) {
// test accordingly
switch ($type) {
case 'author': if (is_author()) $excerpt = true;
break;
case 'category': if (is_category()) $excerpt = true;
break;
case 'cpt': if (is_post_type_archive()) $excerpt = true;
break;
case 'date': if (is_date()) $excerpt = true;
break;
case 'home': if (is_home()) $excerpt = true;
break;
case 'search': if (is_search()) $excerpt = true;
break;
case 'tag': if (is_tag()) $excerpt = true;
break;
case 'tax': if (is_tax()) $excerpt = true;
break;
}
// don't bother continuing to test if we've already decided
if ($excerpt) break;
}
}
if ($excerpt) {
// https://wordpress.stackexchange.com/a/77947/25187
remove_filter('the_content', array($this, 'excerpt_not_content'), 99);
$output = apply_filters('the_excerpt', get_the_excerpt());
add_filter('the_content', array($this, 'excerpt_not_content'), 99);
}
return $output;
}
And, to avoid recursion, remember to remove a filter that is currently altering something before calling a filter that will alter the same something that will call a filter to alter the something that will call a filter to alter …