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.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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!

share|improve this answer
    
Cheers again Jan! - that works. What are the numbers placed in these functions for? ie 17478 –  daniel Crabbe May 17 '11 at 16:45
    
@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 '11 at 18:18
    
good idea & thanks for explaining... Dc –  daniel Crabbe May 19 '11 at 16:01

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.