How to Customize WordPress Excerpts

If you have a theme that displays excerpts on index type pages of your site, read on. Also, if you’ve opted for using custom excerpts instead of WP’s built-in ‘more’ tag, there are a couple excerpt related plugins that will help you make the most of these features. I recommend using this plugin as opposed to the one below, for simplicity’s sake. However, if you want more control (as laid out in the parameters below) then see if this works better for you:

Note: To get the most out of these plugins, be sure you have the Excerpt field enabled: When on any EDIT screen in wp-admin, click ‘Screen Options’ in the top right corner. Make sure there is a check next to Excerpt‘. This will enable you to make manual excerpts.

Advanced Excerpt Plugin

For super quick and flexible, check out the Advanced Excerpt plugin by Bas van Doren. This one can be installed by going to your Plugins>Add New menu and typing ‘advanced excerpt’. The main advantage of this plugin is that you don’t need to alter theme files and there is a settings page with a few handy options.

Remember, if you don’t like where a particular post’s excerpt is cut off, you can use the Excerpt field to create a manual excerpt. Instead of being truncated, ALL text entered in the Excerpt field will be displayed.

Under Settings > Excerpt, make sure the box next to ‘No Custom Excerpts’ is NOT checked.

The Excerpt Reloaded Plugin

The rest of this article details how to install and configure The Excerpt Reloaded plugin, which is not as pluggable as the one above, but is still running solid as the original excerpt plugin and can have finer control over filtering elements out of the raw content, if need be. If the top one doesn’t fit your needs, give this one a try. You will have to edit your theme files, but that is possible through the wp-admin panel…

note: do not use the built-in Plugins>Add-New menu to install this plugin, as for some reason it displays the wrong version. Do it the old fashioned way…
  1. Download the latest version of The Excerpt Reloaded
  2. Activate the_excerpt_reloaded plugin in your WordPress admin panel, under the ‘Plugins’ tab
  3. Replace ‘ <?php the_excerpt(); ?> ‘ or ‘ <?php the_content(); ?> ‘ with ‘ <?php the_excerpt_reloaded(); ?> ‘ in your theme files.
note: You can edit your theme by going to Appearance>Theme Editor, then select the specific file from the drop-down menu. You’ll want to start with index.php, then if there is an archive.php, category.php, etc, replacing the_content with the_excerpt_reloaded. Be sure to check your parameters, as outlined below

Customizing the_excerpt_reloaded Excerpts

The Excerpt Reloaded has the following parameters that can be used to fine tune excerpts:

<?php the_excerpt_reloaded(excerpt_length, ‘allowedtags’, ‘filter_type’, use_more_link, ‘more_link_text’, force_more_link, fakeit, fix_tags); ?>

Parameters:

excerpt_length
(integer) Number of words to display before ending the excerpt. Default is 120.

allowedtags
(string) Defines which HTML tags to retain in excerpt. Use the format ‘<img>’. For multiple tags, enter as single string: ‘<a><img>’. Default is ‘<a>’.

filter_type
(string) Defines how WordPress should filter/format an excerpt’s content. Options are based on content/excerpt tags: ‘content’, ‘content_rss’, ‘excerpt’, ‘excerpt_rss’. Set to ‘none’ to display raw content. Default is ‘excerpt’.

use_more_link
(boolean) Should the “more” link be displayed (TRUE) or not (FALSE). If set to false, function displays ellipsis ([…]) if content more than that displayed; neither link nor ellipsis displays if output is less than excerpt_length; Defaults to TRUE.

more_link_text
(string) If use_more_link is set to TRUE, set this to define what text to use for the link. Default is ‘(more…)’.

force_more_link
(boolean) Display more link (TRUE) or not (FALSE), even when excerpt is less then excerpt_length. Defaults to FALSE.

fakeit
(integer) Use content as excerpt (1) or not (0) if excerpt is empty. Set to 2 to force content as excerpt under all conditions. Defaults to 1.

fix_tags
(boolean) “Repair” HTML tag elements (TRUE) or don’t (FALSE). This is implemented to deal with improperly closed tags which may be caused by excerpt_length. If fix_tags is set to FALSE, the plugin will not attempt XHTML validation and repair on improperly closed tags (due to excerpt_length breaking them in mid-element). Defaults to TRUE.

Example:

<?php the_excerpt_reloaded(23, ‘<img><a>’, ‘content’, true, ‘Read More…’, true);?>

This would give us excerpts 23-words long, allow images and links to render when seen in excerpts, use post ‘content’ to generate.excerpts, use the ‘more’ link, more link text (in this case it’s ‘Read More…’), that we should always see the ‘Read More…’ link, and finally, that any incomplete tags in the ‘more’ text should be fixed.

Optional Excerpt

Using either the default <?php the_excerpt(); ?> or the Excerpt Reloaded tag <?php the_excerpt_reloaded();?> you can also make use of the ‘optional excerpt’ field if you would rather provide a custom excerpt.

When writing or editing a post you can fill in the ‘optional excerpt’ field and this text will be displayed on all your theme pages that use excerpt tags, rather than the excerpt being taken from the full content. This way you can provide custom previews of protected articles and completely secure the actual article. And if you don’t feel like filling in ‘optional excerpt’ it will simply use the content to generate an excerpt.

Erin Bruce