WordPress apply get_excerpt formatting to a string

I would like to apply wordpress’ get_excerpt format to a string I pass to it.

I know the standard excerpt only works within the loop – but I’m hoping there is a function or something I’m missing that generates the excerpt when you don’t manually define one.

Related posts

Leave a Reply

1 comment

  1. Fixed answer:

    You need wp_trim_words(). Yes – you’re right. wp_trim_excerpt() doesn’t play nice with passed strings.

    http://codex.wordpress.org/Function_Reference/wp_trim_words

    wp_trim_words( "I would like to apply wordpress' get_excerpt format to a string I pass to it. I know the standard excerpt only works within the loop - but I'm hoping there is a function or something I'm missing that generates the excerpt when you don't manually define one.", 5, '[...]' ); returns “I would like to apply[…]”

    Previous answer:

    WordPress uses wp_trim_excerpt() to generate excerpts.

    http://codex.wordpress.org/Function_Reference/wp_trim_excerpt

    You probably want some filtering on there too before you output (depending on the source of your text).