Welcome Guest! Login? Checkout

Recently there was an instance where I had to grab the first sentence of a WordPress post. Not the first X amount of words, but the first sentence. From the capital letter to the full stop.

There are many simple ways detailed in many blog posts to change the excerpt length, but they’re generally limited to words. There are functions such as wp_trim_words that will help you trim to an amount of words, but this is a way to go a little bit flexible.

Enter preg_split

Preg_split is a PHP function that allows you to split a string by regular expression. Whenever a string is met, it is split into it’s own index in an array. This allows you to return a number of strings.

There are a number of attributes associated with this function, outside of the usual preg_* functions. One such thing is the limit attribute. This allows you to stop capturing after a certain number of iterations. The second we will use is the PREG_SPLIT_DELIM_CAPTURE. This attribute allows you to capture the string we are searching for.

The Code

So, here is the code in question:-

This function, when passed a string $string, will return the first sentence of the string. It searches for a question mark, exclamation mark or full stop within the string, and then everything before it will be entered into the first index in the array $sentence. The second index is the punctuation mark to close the sentence, and they are combined to returned a string.

You simply have to pass any string to this function for it to work. So if you need a one off excerpt, you can use just winwar_first_sentence( get_the_excerpt() ); somewhere in your template.

Using This In The Excerpt

Should you be using excerpts in posts, you may want to apply such a filter to the post. You can do this using the get_the_excerpt filter. This filter is run on the excerpt, so we can use that to shorten it to one sentence. To do this we add one line to the code above:-

A Word of Warning

If you use the above filter, it will shrink the length of the excerpt throughout the site to the first sentence. As such, if you want to use the variable excerpt site elsewhere, I’d avoid using the filter.

Also, as pointed out by Tim Nash, this will cause issues if there is ever a domain name in your first sentence.

WP Engine Managed WordPress Hosting


Polite Disclaimer: I am welcome, open and willing for corrections to be shared in the comments (with corrections being added to posts and credited), and the comments field should be used to promote discussion and make this post better. I do not know everything and if anybody finds a better way to do something, then by all means please share it below.

However, I'm unable to offer support to posts. The reason being is that WordPress has tens of thousands of plugins and millions of themes. As such, finding out exactly why code doesn't work with your setup is a long process. If you wish for me to look at your code, please use the priority support area.

Comments asking support will be left on the site, but there is no guarantee of answer.

    Comments are closed.