0

I want to be able to remove automatic excerpts (and keep the custom ones) only on single posts since I make use of automatic excerpts in my archives. I am using this code:

if ( is_single() && 'post' == get_post_type() ) {
    remove_filter('get_the_excerpt','wp_trim_excerpt'); 
}

But it doesn't seem to be doing anything. Without the condition, it strips out automatic excerpts from all places where excerpts exist.

3
  • Test each half of the condition independently to see which conditional is failing.
    – t31os
    Commented Oct 19, 2022 at 3:17
  • did you try if (is_singular('post') ) { }?
    – rudtek
    Commented Oct 19, 2022 at 4:31
  • @rudtek Yeah, same issue, it displays automatic excerpts on single posts. Commented Oct 19, 2022 at 10:49

1 Answer 1

1

It seems to me that your code runs too early, i.e. before WordPress determined what the queried object is, i.e. whether it's for a single post, a category archive, a search results page, etc.

Such issue could happen if you added the code "just like that" to your theme's functions file, so try doing it like so instead where I put the code inside a function which is hooked on wp:

add_action( 'wp', function () {
    if ( is_single() && 'post' == get_post_type() ) {
        remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
    }
} );
1
  • 1
    You are absolutely right, it works. Thanks for the observation and solution! Commented Oct 19, 2022 at 16: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.