The get_the_excerpt() entry in the WordPress Codex says that it returns “…either a user-supplied excerpt, that is returned unchanged, or an automatically generated word-counted trimmed-down version of the full post content” (although the more up-to-date WordPress Developer Resources entry has no such language). I find that calling get_the_excerpt() doesn’t work when a post has post content but not a post excerpt. It just returns an empty string, without trying to trim down the post content to automatically create an excerpt as the Codex description would lead you to believe.

The function below can be used to get the excerpt for a post, whether it has a post excerpt already, or it has to generate one using the post content. You can limit the number of words in the excerpt by passing in a $num_of_words argument.

Leave a reply

Your email address will not be published. Required fields are marked *