• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Draft / Full Post on Blog Archive

Full Post on Blog Archive

Last updated on October 15, 2018 by Sal Ferrarello

On a WordPress project, I wanted to display the full post on the blog archive unless a manual excerpt had been created for the post (in which case I wanted to use the manual excerpt).

I’m a big fan of the Genesis framework and in the Genesis theme settings under Content Archives there is a Display option where you can choose between:

  • Entry content
  • Entry excerpt
Screenshot of Genesis Content Archives Display Entry Content Setting

Genesis Settings Screenshot

The problem here is when you choose Entry content, the content is used regardless of whether or not the post has a manual excerpt. In my case, I wanted a manual excerpt to override displaying the Entry content.

Solution

Step 1: Remove the Default Excerpt

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );

Step 2: Add Our Own Excerpt Filter

add_filter( 'get_the_excerpt', function( $excerpt ) {
    if ( $excerpt ) {
        // If an excerpt already exists (e.g. manual excerpt) display it.
        return $excerpt;
    }
    // Use the full post content as the excerpt.
    return get_the_content();
});

Full Solution

This is the code I place in my home.php template because I only wanted this behavior on the Posts Page (a.k.a. blog page). You could place this code somewhere else (e.g. a mu-plugins file or functions.php, see functions.php vs plugin vs mu-plugin for WordPress for more information on these choices).

/**
 * Use the full post content as the excerpt
 * unless a manual excerpt exists.
 */
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', function( $excerpt ) {
    if ( $excerpt ) {
        // If a excerpt already exists (e.g. manual excerpt) display it.
        return $excerpt;
    }
    return get_the_content();
});
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Draft, Programming, Solution Tagged With: excerpt, WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in