The standard way of getting an excerpt is to use the_excerpt()
or get_the_excerpt()
template tags. I am trying to get just the actual content of the Excerpt field.
If there is an excerpt, I want to display it in full (without being abridged or appending […]). If there is no excerpt, I don’t want anything to be displayed.
Is there a straightforward way to do this in WordPress?
Something like this:
$real_excerpt = ???
if ( $real_excerpt ) {
echo $real_excerpt;
} // shouldn't show anything if there isn't a custom excerpt
Why don’t you use the global
$post
variable? It contains an object with the content as it is on the db row corresponding to that post. Here’s how to use it:Or:
Very simple, but let us know if you’re having trouble getting it working.
Tracing back:
the_excerpt()
When you look at the source of
the_excerpt()
, then you’ll find the following function definition:This means, that
get_the_excerpt()
holds the plain, unfiltered content.get_the_excerpt()
When you then look at the source of
get_the_excerpt()
, you’ll find the following:So there’re again filters added to
get_the_excerpt()
.Default filters &
wp_trim_excerpt()
All the core filters, that are attached to something, can be found inside
~/wp-includes/default-filters.php
.There you’ll find (with WP version 3.4), the following filter:
wp_trim_excerpt()
on Line #147.The
wp_trim_excerpt()
function looks like the following from its inside:What are our options?
You could use each of those functions with all their unwanted filters, by simply removing the filters. But this also means, that you’d be removing them from everything else as well.
Calling the plain
->excerpt
, gives you an excerpt in every case – except if there is none. Which means, that you can insertscripts
andCDATA
tags as explained in this answer, but will also have to deal with the post password check, as well as moving back in all the filters that you need.