Introducing Extended Excerpts
by Luke America on April 18, 2011
Last month, we featured an article entitled Introducing Smart Excerpts. That technique provided an alternative for the WordPress functions get_the_content() and get_the_excerpt() offering configurable enhancements … including the number of paragraphs to display (instead of the number of words).
Today, we present three filters that will allow you perform some of those features. This set of filters actually modifies the get_the_excerpt() and the the_excerpt() behavior adding additional capabilities. It's intended use is for a theme's search.php page. But, it can be implemented on other pages.
You can place the three filters in your theme's functions.php file. If all you want to do is change the excerpt length and the excerpt link, comment line 5 in the following code segment.
1 2 3 4 5 | add_filter('excerpt_length', 'wcs_excerpt_length'); add_filter('excerpt_more', 'wcs_excerpt_more'); // comment next line to use fully stripped excerpts add_filter('get_the_excerpt', 'wcs_excerpt_extended'); |
The following filter allows you to bypass the default 55 word limit for excerpts. Set the value to your preference.
1 2 3 4 5 | function wcs_excerpt_length($length) { // default length = 55 return 75; } |
This next filter changes the excerpt appended [...] text to an actual link to the article (post). Change the text on line 6 to your preference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function wcs_excerpt_more($more) { // init global $post; $more_link_default = '[...]'; $more_link_text = '(read more)'; // construct the link $title = get_the_title($post->ID); $link = '<a href="' . get_permalink($post->ID) . '" title="' . $title . '">'; $link .= $more_link_text . '</a>'; // exit return str_replace($more_link_default, $link, $more); } |
This last one more extensively modifies the default excerpt behavior … and it uses the two previous filters. The primary reason to use this particular filter is that it allows some HTML tags (boldface, italics, and links) to be included in the excerpt. But, because of the way words are extracted in excerpts, you are limited in the workable tags that are allowed.
By default, this filter processes shortcodes. If you want to strip them (the way that normal excerpts do), UNcomment line 13 and comment line 14 instead. The remainder of the code is nearly identical to standard excerpt processing except that we also make use of our other two filters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | function wcs_excerpt_extended($content = false) { // init global $post; $content = $post->post_excerpt; $allowed_tags = '<i>,<em>,<b>,<strong>,<ul>,<a>'; $more_link_default = '[...]'; // extract the content $content = $post->post_content; // comment ONE of the next two lines //$content = strip_shortcodes($content); $content = do_shortcode($content); // process the content $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content, $allowed_tags); // extract the words $excerpt_length = wcs_excerpt_length(55); $words = explode(' ', $content, $excerpt_length + 1); if(count($words) > $excerpt_length) { array_pop($words); array_push($words, $more_link_default); $content = implode(' ', $words); $content = wcs_excerpt_more($content); } // force complete HTML tags $content = balanceTags($content, true); // exit $content = '<p>' . $content . '</p>'; return $content; } |
Remember to insert the code snippet and the three filters above into your theme's functions.php file.
Comments
Share Your Thoughts 7 Responses to “Introducing Extended Excerpts”Trackbacks
Check out what others are saying about this post...-
[...] Hot Article: How To Fix the 3.1 Custom Permalinks BugClick an Article Link To View It:Introducing Extended ExcerptsPlugin Review: XML Sitemaps for MultisitesHow to Build an ‘Archives by …Easy Tips to [...]

Subscribe to our RSS feed
Follow Us on Twitter



curious which syntax highlighter do you use? been looking for something to highlight wp code (while linking to the functions)
Hi Chris. This is one that I wrote. I’ll be submitting it to WordPress soon. Would you be interested in beta testing the final version before it goes “public”?
It’s called “wcs-syntax-highlighter-xl”. It works using a shortcode [wcs_syntax] … supports dozens of languages … uses a CSS file for colors, etc. … and has many options configurable thru the shortcode.
ya i would love to test it, currently im just using wp-syntax.
As you may have noted, it supports “PHP” and also “PHP with WordPress” for the visitor function-lookup feature.
This is good, but I like your smart quotes function better.