Introducing Smart Excerpts
by Luke America on March 20, 2011
Many themes only provide two choices for displaying a blog's posts … either the full content or an excerpt. Excerpts are the first 55 words of the content with all of the HTML tags stripped. This latter option is great for searches, but it leaves much to be desired for the front page of your blog.
You'll see my alternative when you view All Articles in our main menu (top of the page), category lists, tag lists, etc. Scrolling down the page, you'll note that the first paragraphs of each post is presented in excerpt blocks.
Our Smart Excerpt function grabs the first two paragraphs (by default, more if you specify it) of a post's content leaving many of the HTML tags unscathed. This includes images, paragraph breaks, boldface, italics, and a few others. It also processes any shortcodes and smilies present in those first two paragraphs.
This is an ideal solution for our blog. Maybe it will be useful to you too. First, let's look at the PHP source code and then we'll explain where to use the function in your theme files.
1 2 3 4 5 6 7 | function wcs_smart_excerpt( $more_link_text='[...]', $max_paragraphs=2, $allowed_tags='<i>,<em>,<b>,<strong>,<ul>,<blockquote>,<a>,<img><p><br>') { echo wcs_get_smart_excerpt($more_link_text, $max_paragraphs, $allowed_tags); } |
We provide two versions of the functionality. The first option (above) grabs the content AND echos the output. The second one (below) does most of the work and does NOT echo the output … you can read it's output into a PHP string variable. This is a technique that's employed with many of WP's own functions.
As you can see wcs_smart_excerpt has three possible parameters, each with default values … so you can omit them if you prefer the defaults. $more_link_text is the link text that's displayed to invite the visitor to continue reading. $max_paragraphs is the number of paragraphs to display … a minimum of one is required. And, $allowed_tags is a comma-separated list of HTML tags to NOT strip out. You only need to enter the opening tag name as is illustrated in the default value.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | function wcs_get_smart_excerpt( $more_link_text='[...]', $max_paragraphs=2, $allowed_tags='<i>,<em>,<b>,<strong>,<ul>,<blockquote>,<a>,<img>,<p>,<br>') { // MUST be used in the loop // (typically in: home.php, archive.php, category.php, and/or tag.php) // where desired, replace the "the_content(" or "the_excerpt(" with "wcs_smart_excerpt(" // by default, most notably these tags are stripped: // <div>, <span>, <object>, <embed>, <table>, <tr>, <td>, & <h1> ... <h6> // init global $post; $paragraphs = array(); if ($max_paragraphs < 1) {$max_paragraphs = 1;} $max_paragraphs--; // use the blogger-specifed excerpt if it exists if(strlen($post->post_excerpt) > 0) { $output = get_the_excerpt(); return $output; } // otherwise, get the content $content = trim(get_the_content()); // replace galleries in the excerpt with a message $content = preg_replace('#\[gallery*?\]#is', '<p><p><br/>' . __('(photo gallery)') . '<br/></p></p>', $content); // uncomment the next line to omit all shortcodes //$content = preg_replace('#\[.*\]#is', '', $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content, $allowed_tags); // extract desired paragraphs $output = ''; $paragraphs = explode('</p>', $content); for ($i = 0; $i <= $max_paragraphs; $i++) { $output .= $paragraphs[$i]; } // post-process the extracted content $output = balanceTags($output, true); if ($max_paragraphs < sizeof($paragraphs)) { $title = get_the_title(); $output .= '<a href="' . get_permalink() . '" title="' . $title . '">'; $output .= $more_link_text . '</a>'; } // exit return $output; /********************************************************************** Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info) you can redistribute and/or modify this code under the terms of the GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html **********************************************************************/ } |
After the initialization, we abort the process IF a specific post has a user-provided excerpt. This aspect is part of what makes it smart … returning the blogger's excerpt if one has been typed, lines 19 – 24.
In lines 26 – 34, we grab a post's content … apply normal processing … and strip unwanted HTML tags. Then in lines 36 – 42, we begin building the output by including only the desired number of paragraphs. We finish up by adding the “continue reading” link. Quick and simple.
Where to Use It in Your Theme Files
Typically, you'd utilize this function in these theme files: home.php, archive.php, category.php, and/or tag.php. These four display lists of posts.
In many themes, these files will contain a line (inside the loop) like this:
<?php the_content('Continue Reading'); ?><div class='clear'></div>
Or:
<?php the_excerpt(); ?><div class='clear'></div>
Simply comment out that line, and add one like this:
<?php wcs_smart_excerpt('Continue Reading'); ?><div class='clear'></div>
That's it. After implementation, you may want to change your blog's list post count (Dashboard -> Settings -> Reading).
You might also find useful the three filters for the get_the_excerpt() function discussed in Introducing Extended Excerpts.
Comments
Share Your Thoughts 2 Responses to “Introducing Smart Excerpts”Trackbacks
Check out what others are saying about this post...-
[...] month, we featured an article entitled Introducing Smart Excerpts. That technique provided an alternative for the WordPress functions get_the_content() and [...]

Subscribe to our RSS feed
Follow Us on Twitter



hi this is not working at all…