How to detect if manual excerpt exists in WordPress?

I need to show excerpts of different lengths, so I use

function custom_excerpt($length) {    
get_the_content();
... clean up and trim
return $excerpt;
}

however, I want to detect if a manual excerpt was entered in order to use that instead of the custom one. Is there a way to do this?

Read More

I tried by using

$wp_excerpt = get_the_excerpt();

But that returns the manual excerpt, and if the manual excerpt is empty, it automatically generates an excerpt of 55 characters, which doesn’t help, because it will always be “true” (can’t check if empty).

The reason for approaching it this way is because I have multiple excerpts on a single page (different lengths), and if the length needed is longer than the WordPress excerpt (55), I want to show my excerpt, unless a manual excerpt was written, in which case I want to show that.

It would be perfect if I could simply

if ( manual_excerpt() == true ) {
}

Related posts

Leave a Reply

3 comments

  1. You need only replace the excerp_length wordpress default function, just follow the above code, then you can call this custom function and set the length:

    <?php
    // custom excerpt length
    function custom_excerpt_length( $length = 20 ) {
       return $length;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    ?>   
    

    ANSWER UPDATED II

    Using inside a function:

    <?php
    function custom_excerpt( $length = 55 ) {    
        if( $post->post_excerpt ) {
            $content = get_the_excerpt();
        } else {
            $content = get_the_content();
            $content = wp_trim_words( $content , $length );
        }
        return $excerpt;
    }
    ?>
    
  2. This is an old question but I was looking for this, ended up here and didn’t see the following function in the answers. To know if a post has a custom excerpt you can use the has_excerpt function:

    <?php has_excerpt( $id ); ?>
    

    Where $id is the post id. If non is given then the current post id will be used.

  3. Check if the post_excerpt slot in the post object is empty or not:

    global $post;
    
    if( '' == $post->post_excerpt )
    {
        // this post does NOT have a manual excerpt
    }
    

    To turn this into a function:

    function so19935351_has_manual_excerpt( $post )
    {
        if( '' == $post->post_excerpt )
            return false;
    
        return true;
    }