1

How do I adjust the post excerpt when the excerpt itself is blank / empty? I looked and the code for the the_excerpt filter tests for empty and if the excerpt is empty returns before apply_filters is called which probably makes sense for most cases. I want to set a "default" excerpt if possible.

https://core.trac.wordpress.org/browser/tags/4.7/src/wp-includes/post-template.php#L380

I'm assuming there is another way to adjust the excerpt but I have yet to find it. Thank you!

Per Vinod Dalvi's request I'm adding some details. I'm using this on a child theme of twentysixteen (v1.2) but have just now tested this using only the parent theme on a mostly blank WordPress install. I'm using the following code:

add_filter( 'the_excerpt', function( $excerpt ) {
    die( 'the_excerpt filter was called' );
} );

Using the above code I'm still only able to see the filter being called when the excerpt field is not empty and again per the linked code above it appears there is a test for empty and a return before the filter is applied.

I found my issue. The theme twentysixteen (v1.2) uses the function twentysixteen_excerpt which calls the function has_excerpt to test for the excerpt before calling the_excerpt function which is what fires the filter the_excerpt. The test for empty is only on the filter get_the_excerpt which I originally was using hence at least part of my confusion. So Vinod Dalvi answer is correct depending on what theme you're using.

Since the original question was not in the context of the twentysixteen theme and even though Vinod's answer is correct (most of the time) I felt the need to add that if you want to make this work for the twentysixteen theme you need to override the template-parts/content-single.php template in your child theme and remove twentysixteen_excerpt (L16) in favor of just the_excerpt() (function).

  • 1
    get_the_excerpt() actually checks for an empty WP_Post object. The actual excerpt can be blank. – Nathan Johnson Apr 23 '17 at 0:01
1

If you are talking about excerpt set manually in the excerpt field of post then you can use following code in the functions.php file of your child theme or plugin to display default excerpt if no excerpt it set.

function display_default_excerpt( $excerpt ) {

    if ( has_excerpt() ) {
        return $excerpt;
    } else {
        return __( 'There is no excerpt so displaying default excerpt.' );
    }
}
add_filter( 'the_excerpt', 'display_default_excerpt' );
  • Yes, this is exactly what I'm trying to accomplish except the_excerpt is only called if the excerpt has been "manually" entered (has some value). – JSP Apr 23 '17 at 12:00
  • @JSP I am using in my code the_excerpt filter which always gets called and i have tested it on my test site. Please test the shared code yourself. – Vinod Dalvi Apr 23 '17 at 12:40
  • Thanks for chiming in. Not sure why that would work for you or what I'm missing but I added more detail to the original question in the hopes that someone will point out the mistake I'm making. – JSP Apr 23 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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