Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers

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:

global $post; // If for some reason it's readily accessible, invoke it
if($post->post_excerpt != '') {
    echo($post->post_excerpt);
}

Or:

$my_post = get_post($post_id);
if($my_post->post_excerpt != '') {
    echo($my_post->post_excerpt);
}

Very simple, but let us know if you're having trouble getting it working.

share|improve this answer
    
While Kaiser's thoughts provided a lot of insight, your first snippet was enough to create a simple function so selectively display the unfiltered excerpt. Thanks! –  mattrepublic Dec 18 '12 at 18:37
add comment

Tracing back:

the_excerpt()

When you look at the source of the_excerpt(), then you'll find the following function definition:

function the_excerpt() {
    echo apply_filters('the_excerpt', get_the_excerpt());
}

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:

function get_the_excerpt( $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );

    global $post;
    $output = $post->post_excerpt;
    if ( post_password_required($post) ) {
        $output = __('There is no excerpt because this is a protected post.');
        return $output;
    }

    return apply_filters('get_the_excerpt', $output);
}

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:

function wp_trim_excerpt($text = '') {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

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 insert scripts and CDATA 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.