Show Excerpts Instead of Full Posts on Archives
Shows a short excerpt and a "Read more" link for each post on the blog page, archives and search results, for themes that print the full content of every post.
Opens WPIDE on
Code
add_filter( 'the_content', function ( $content ) {
if ( is_singular() || is_feed() || ! in_the_loop() || ! is_main_query() || doing_filter( 'get_the_excerpt' ) ) {
return $content;
}
static $running = false;
if ( $running ) {
return $content;
}
$running = true;
$excerpt = get_the_excerpt();
$running = false;
return '<p>' . esc_html( $excerpt ) . '</p><p><a class="more-link" href="' . esc_url( get_permalink() ) . '">Read more<span class="screen-reader-text"> about ' . esc_html( get_the_title() ) . '</span></a></p>';
}, 1 );
Notes
The excerpt length and the link text follow your excerpt settings and other excerpt snippets.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.