Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.