I’m building a theme that is going to show excerpts on the homepage for potentially dozens of posts. I don’t have manual excerpts on all of my posts, so
$post->post_excerpt is empty for many posts. In the event that there isn’t a manual excerpt, I would like to use the built-in get_the_excerpt() function, but it isn’t available outside the loop.
Tracking down the function, it looks like it uses wp_trim_excerpt from wp-includes/formatting.php to create excerpts on the fly. I am calling it in my code like
wp_trim_excerpt( $item->post_content ), but it is simply returning the full content. Am I doing something wrong?
I know that I can create my own function to create an excerpt, but I like to use built-in functions where possible, keeping my code compatible with other potential plugins / filters.
Since WP 3.3.0,
wp_trim_words() is helpful if you’re able to get the content that you want to generate an excerpt for. Hope that’s helpful to someone and it saves creating your own word counting function.