excerpt – don’t use main content if empty

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?

Read More
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

Related posts

Leave a Reply

1 comment

  1. 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!