WordPress Excerpt is an optional summary of your posts. In this tutorial, I will show you how to automatically control the Manually typed excerpt AND the Automatically generated excerpt at the same time, by using a simple function and without the need of a plugin.
The image below shows the result when using CODE-Y below. Notice that both the Manually typed Excerpt and the Auto-generated excerpt are present.
This is my Fifth tutorial related to the WordPress Excerpt. My 4 previous tutorials are:
- Create a Variable Length Excerpt in WordPress Without a Plugin.
- 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.
The relationship between the Manual Excerpt, Automatic Excerpt, and Teaser is this: When a post has no manual excerpt added to the dashboard and the post template uses the_excerpt() tag, WordPress automatically generates an excerpt by selecting the first 55 words of the post followed by the unlinked ellipsis “[...]”. When the post template uses the_content() tag, WordPress will look for the More tag and create a teaser from the content that precedes the More tag. However, if a post has a manually added excerpt, then WordPress, by default, will display the content of the manual excerpt instead. However, in this case, the manually added excerpt will be returned untouched, with formatting if it exists. It will not be shortened.
Where To Add the Manually Typed Post Excerpt
If you don´t know where to add a manual excerpt, then read on …
To manually add an excerpt to a post, you need to login to your WordPress dashboard, find your post, and add the excerpt as shown in the picture below:
The Advantage of a Manual excerpt is that it is often used by search engines to describe search results. In this CASE, good excerpts increase Website traffic. Another major drawback of the auto-generatic excerpt is that the beginning of a post is usually an introduction and not a summary.
Auto-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 the text will be unformatted. This makes the excerpt one text paragraph without any line breaks.
CODE-Y below has several features:
- No extra Plugin to your Blog. Caution about adding plugins to your theme.
- The code takes care of both scenarios: The manually added excerpt and the automatically generated excerpt. You can have both a manual excerpt on one post and and auto-generated excerpt on another post.
- Option to add a linked anchor to the end of the Excerpt.
- Option to add an un-linked Ellipsis ([…]) to the end of the Excerpt.
- Option to control the excerpt length.
- However; the code strips all HTML formatting in the excerpt.
Automatically Control the Manual Excerpt
To add CODE-Y: Open functions.php file located in your current theme´s folder and add (copy and paste) the following CODE-Y. 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.
CODE-Y works for both the manually typed excerpt AND The automatically generated Excerpt. If there is a manual excerpt in the dashboard it will be displayed at the main page of the blog. If there is no manual excerpt added, then the auto-generated excerpt will be displayed at the main page of the blog.
For the Auto-generated excerpt: 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 it stops at your defined excerpt length whichever comes first.
For the Manually typed excerpt: Watch out: If you type a shorter excerpt in your dashboard, than the length defined in the code (line 22 of
CODE-Y), then the excerpt displayed will not show the excerpt_end as defined in line 25 of CODE-Y.
CODE-Y works on WordPress 3.3 and higher. But I hope that you will always upgrade to the latest version.
CODE-Y does NOT preserve any HTML formatting in the Excerpt, be it manual or the auto-generated one.
How To Change the Excerpt Length in CODE-Y
CODE-Y is set at the Excerpt length of 45 words. If you like to change the Excerpt length, then find line 22 and replace it with the integer that you wish. Keep the length reasonable.
How To Replace the Excerpt More in CODE-Y
CODE-Y is set to add a ´Continue Reading´ link to the end of the Excerpt. 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, which is the default, then replace Line 25 of CODE-Y with the following:
$excerpt_end = '[...]'; //This is the WP default.
CODE-Y Does Not Work for my Theme
If CODE-Y is not responding in your theme, make sure that the_excerpt() function exists in your theme´s index.php file. You can also check its existence in archive.php and search.php files.
How To Replace the_content() With the_excerpt()
Never show full posts in the main page. In your theme, If you have the_content() template tag, make sure to use the More tag in your post. They work together, otherwise your posts will be shown in full in the main page which can dramatically increase page loading time. One way to reduce page load time, is to replace the_content() template tag with the_excerpt() template tag.
To do that, open the index.php file located in your theme´s folder and replace the_content() with the_excerpt(). You could also do the same for archive.php and search.php files. (DO NOT TOUCH page.php or single.php files.)
NOTE: the_excerpt() template tag has no parameters. However, there is no harm from keeping them in case you wanted to revert back and use the_content() again. If you get any errors, then replace the_content(‘…’); with the_excerpt();.
To EMPHASIZE: the_content() template tag is used in conjunction with the More tag. Make sure to place the More tag somewhere at the beginning of the post. Otherwise, the whole content of the post will be displayed.
In this tutorial, I showed you how to automatically control the Manually typed Excerpt AND also the Auto-generated excerpt at the same time, without the need of a plugin.
If you have any questions or anything else to say, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.