WordPress Excerpt is a summary of your posts. In this tutorial, I will show you how to generate variable Length Excerpt that completes the sentence. Meaning that the Excerpt is not cut off mid-sentence. Also, I will show you how to preserve All, Some or None of the HTML formatting in the Excerpt, among several other features listed below.
WordPress Excerpts are generated from the post content by a WordPress filter using the function wp_trim_excerpt(). This function located in wp-includes/formatting.php of your core WordPress code.
This is my Fourth tutorial related to the WordPress Excerpt. My 3 previous tutorials are:
- Remove Header Tags and Their Content in WordPress Excerpt Without a Plugin.
- Preserve HTML Tags in WordPress Excerpt Without a Plugin.
- Improve WordPress Excerpt Without a Plugin.
Manually Typed Post Excerpt
If you don´t know where and how to add a manual excerpt, read the section “How To: Manually Add a Post Excerpt” in this tutorial.
Automatically Generated Post Excerpt
In WordPress, if you do not provide a manually typed excerpt to a post, WordPress will display an automatically generated excerpt. By default, WordPress display the excerpt with the first 55 words of the post´s content, an un-linked ´[...]´ string at the end, and with all HTML tags stripped. This makes the excerpt one text paragraph without any line breaks.
CODE-Z below has several features:
- No additional Plugin to your Blog. Caution about adding plugins to your theme.
- Option to generate a variable Length Excerpt. You have the option to complete the sentence.
- Option to generate a fixed Length Excerpt. You have the option to stop at exactly the defined excerpt´s word length.
- Option to add a ´Continue Reading´ link to the end of the Excerpt.
- Option to add an un-linked Ellipsis to the end of the Excerpt.
- Option to preserve ALL, SOME or NONE of the HTML formatting in the Excerpt.
- The Code counts “real words”. The code does not count the HTML tags or their content.
- The Advantage of step 7: No opened HTML tags in the Excerpt. No validation errors, or weird page rendering.
- The Code ignores Manual Excerpts and use the auto generated ones instead.
- The excerpt output can be uniquely styled if desired. For an example, see
Output a Variable Length Excerpt
The image below shows the result when using CODE-Z below. Notice that there are NO weird cuts in the Excerpt, the sentence is complete. Also notice that CODE-Z preserve all HTML formatting in the excerpt, and adds the ´Continue Reading´ link to the end of the Excerpt.
To add CODE-Z: Open functions.php file located in your current theme´s folder and add (copy and paste) the following CODE-Z. Save the file and upload it to your server.
Note: To scroll within the code: You can also click on the code window and use your keyboard´s arrow keys.
Watch out: If you are using the <!–more–> tag in your post content, make sure that it resides outside your defined excerpt length. If there is a More tag in your post, then the excerpt word count stops at either where the More tag is placed. Or depending on your parameters, it stops at your defined excerpt length, or at the end of the sentence, whichever comes first.
The sentence is considered as complete, when it ends with a dot, a Question mark, or an Exclamation mark.
CODE-Z works on WordPress 3.0 and higher. But I hope that you will always upgrade to the latest version.
To test the Regular Expression on line 30 and Line 41, you could use this Regular Expression Test Tool.
CODE-Z ignores the manually typed excerpt. The code works only for the automatically generated Excerpts. If you want to output the manually typed excerpt for your posts, then this code is not for you (or at least the code needs to be modified).
CODE-Z preserves ALL HTML formatting in the Excerpt.
Acknowledgement: Thanks to basvd, some sections of this code has been used from the Advanced Excerpt Plugin.
How To: Preserve Some HTML Formatting in CODE-Z
CODE-Z preserves ALL HTML formatting in the Excerpt. If you want to keep some defined HTML tags. For example, if you like to keep the p, a, strong tags then Comment Out lines 76 and 80 respectively of CODE-Z. Add any other tags that you want to allow separated by a comma.
How To: Remove All HTML Formatting in CODE-Z
CODE-Z preserves ALL HTML formatting in the Excerpt. If you want to disallow ALL HTML tags, then Comment Out lines 79 and 80 respectively of CODE-Z. In this case the excerpt will be displayed as unformatted text. The only exception is that new lines are preserved.
How To: Change the Excerpt Length in CODE-Z
CODE-Z is set at the Excerpt length of 55 words, plus as many more words as needed to complete the sentence. If you like to change the auto-generated Excerpt length, then find line 20 of CODE-Z and replace it with the integer that you wish. Keep the length reasonable.
How To: Replace the Excerpt More in CODE-Z
CODE-Z is set to add a ´Continue Reading´ link to the end of the Excerpt (as shown in the image above). Of course you can change the anchor to something else like ´Read More´. If you want WordPress to output an un-linked “[...]” at the end of the excerpt, then Comment line 53 and Comment Out line 56 respectively.
How To: Set the Excerpt to a Fixed length in CODE-Z
CODE-Z is set to generate a variable Length Excerpt. Meaning that the sentence will be completed, and not cut in the middle. If you want to output a fixed Length Excerpt that stops at exactly your defined word length, then replace integer 1 by 0 in Line 23.
In this tutorial, I showed you how to generate variable Length Excerpt that completes the sentence, with no weird-cuts. The code also counts “real words”, it does not count the HTML tags or their content with the advantage of NO opened HTML tags in the Excerpt, and no validation errors. In addition, I showed you how to preserve ALL, SOME or NONE of the HTML formatting in the Excerpt. I also revisited the excerpt_more and excerpt_length filters.
Do you have any questions? Or anything else to say? If so, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.