Last updated on January 31, 2010. Tags: post excerpt
The WordPress post excerpt is a rarely used feature wherein you can provide short summary or teaser for your post such as that summary part in italics before this paragraph. It is used mainly in category, tag and archive pages as well as in search results.
The text area where you can type the post excerpt is located in the post edit interface immediately below the content edit field. You can save the post excerpt by opening the post edit interface of your post, type the post excerpt in the excerpt text area and click save (even if you didn't change anything in the actual post content).
Similar to category description, post excerpt are not readily available in several WordPress themes. Most WordPress themes display entire posts in the category page, archive page, tag page and search results. You need to manually tweak the PHP codes of these themes to be able to display post excerpts, and that is what we will do in this tutorial.
Most bloggers prefer to either have entire posts showing in the category, tag and archive pages and search results, or use the more quicktag (the one that cuts-off the content with a Read More hyperlink) than to write another paragraph to serve as excerpt. This is likely the reason why most WordPress themes do not readily display the post excerpts.
To display the excerpt in search results, category pages, archive pages, tag pages and home page (list of recent posts), open the index.php and look for the following line of code:
<?php the_content(); ?>
There are usually some stuff between the parentheses in the codes above (e.g., <?php the_content('(read more...)'); ?>). Regardless of what is between the parentheses, replace the entire line with the following:
<?php the_excerpt(); ?>
Note that some themes have separate theme files for search results (search.php) and archive pages (archive.php) but these files contain basically the same PHP code as in index.php so you can simply modify these files in the same way you modified the index.php.
When the posts was listed in category, tag, archive or search results, the excerpts for each posts is shown under the title in place of the content.
If you enabled the excerpt but left the excerpt text area of a post blank, the excerpt that will appear in its place will be formed by the first 55 words of the posts and ending with [...]. HTML tags and graphical elements that are within the first 55 words are removed from the excerpt.
However, the excerpt text area itself accepts HTML elements (even though it has no WYSIWYG mode similar to the post content). Hence, you can embed image in the excerpt. You can use this to turn your WordPress website or blog into a photo album, in which you embed thumbnail image on the excerpt and full image on the post content.
You should not use HTML elements in post excerpt if you are using it as meta description because those HTML elements will appear in the header section where they should not be implemented.
By default, the PHP code <?php the_excerpt(); ?> insert paragraph tags <p> and </p> at the start and end of the excerpt respectively. If you disabled the automatic insertion of paragraph tags using filters (say, if you're also using post excerpt as meta description), you can retain the original appearance of post excerpt as seen in WordPress dynamic pages by manually inserting the paragraph tags before and after the code as shown below.
<p><?php the_excerpt(); ?></p>
NOTE: Some WordPress themes have no separate search.php. In such themes, the search function is also handled by index.php. As of this moment, I still haven't tried enabling the excerpt in these kinds of WordPress themes. Maybe you would like to share if you already did.
Posted by Greten on March 7, 2009 under WordPress tweaks
Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
* Required. Your email will never be displayed in public.