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?
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 ) {
}
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:
ANSWER UPDATED II
Using inside a function:
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:
Where
$id
is the post id. If non is given then the current post id will be used.Check if the
post_excerpt
slot in the post object is empty or not:To turn this into a function: