2

trying to stop get_the_excerpt() from defaulting to the_content() if its empty.

this kinda works - well it seems to return 'xxx' so i think has_excerpt() isn't working?

function get_link_excerpt(){
    if(has_excerpt()){
        $LinkExcerpt = get_the_excerpt();
        return $LinkExcerpt."...";
    }
    return 'no excerpt'; 
}
add_filter('get_the_excerpt', 'get_link_excerpt');

what's the best way to control this?

best,Dc

1 Answer 1

4

WordPress sets up a default filter for get_the_excerpt: wp_trim_excerpt(). It is this function that will generate an excerpt from the content "if needed". If you don't want this behavior, you can just unhook the filter:

add_action( 'init', 'wpse17478_init' );
function wpse17478_init()
{
    remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
}

Now get_the_excerpt() will just return the contents of the post_excerpt database field. If you want to return something when it is empty, you only need to check this case:

add_filter( 'get_the_excerpt', 'wpse17478_get_the_excerpt' );
function wpse17478_get_the_excerpt( $excerpt )
{
    if ( '' == $excerpt ) {
        return 'No excerpt!';
    }
    return $excerpt;
}

There is no need to call get_the_excerpt() - it could even introduce an endless recursion because it applies your filter again!

2
  • Cheers again Jan! - that works. What are the numbers placed in these functions for? ie 17478
    – v3nt
    May 17, 2011 at 16:45
  • 1
    @daniel: To make sure they will be unique when someone copies and and pastes this code in their own installation, I prefix all function names with wpse (WordPress Stack Exchange) and the number of the question.
    – Jan Fabry
    May 17, 2011 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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