WordPress.org

Ready to get started?Download WordPress

Forums

Call to get_the_excerpt() on an excerpt-less post invokes the_content (2 posts)

  1. uberdose
    Member
    Posted 7 years ago #

    Me thinks this should not be :). If a post doesn't have an excerpt, get_the_excerpt() shouldn't invoke the filter for the_content.

    E.g., if you have Alex King's Share This plugin installed, get_the_excerpt() will return "Share This" on posts without excerpt.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    No, this is intended behavior. Calling get_the_excerpt() when there is not one will cause WordPress to fake it. It gets the content and then cuts out everything but the first 55 words of it, adds a [...] to it, and returns that.

    the_content filter gets applied to the content before it trims off the end. It has to do this, because the raw content may contain things that control formatting or replacement of words and such and you don't want those to be bypassed just because you're getting an excerpt.

    This is why it's generally a bad idea for plugins (including Share This) to use the_content filter to add things to display with the post but which are not part of it. Yes, a lot of plugins do it, because it's easy. That doesn't make it the correct thing to do.

    If you don't want it to fake the excerpt, do this before your get_the_excerpt() call:
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');

    Make sure to do an add_filter if you need that behavior back afterwards.

Topic Closed

This topic has been closed to new replies.

About this Topic