Better WordPress Post Excerpt – Revamped

April 22nd, 2010

My earlier post Better WordPress Post Excerpt talked about a way to improve upon the default wordpress post excerpt by utilizing a plugin called the Excerpt Rereloaded. Luca Biagini wrote a great plugin that is all well and good for most people, but what about theme developers that want to include better excerpt functionality in their theme without depending on plugins?

So what we’re going to do is integrate the functions of the excerpt rereloaded plugin into our theme file so that it works without ever installing the plugin.

I have modified the code just slightly, but the functions are all still the same as the original plugin.

In your theme’s functions.php, paste:

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
<?php //make a better wordpress excert
function the_news_excerpt($words = 40, $link_text = 'Continue reading this entry &#187;', $allowed_tags = '', $container = 'p', $smileys = 'no' )
{
global $post;
 
if ( $allowed_tags == 'all' ) $allowed_tags = '<a>,<i>,<em>,<b>,<strong>,<ul>,<ol>,<li>,<span>,<blockquote>,<img>';
 
$text = preg_replace('/\[.*\]/', '', strip_tags($post->post_content, $allowed_tags));
 
$text = explode(' ', $text);
$tot = count($text);
 
for ( $i=0; $i<$words; $i++ ) : $output .= $text[$i] . ' '; endfor;
 
if ( $smileys == "yes" ) $output = convert_smilies($output);
 
?><p><?php echo force_balance_tags($output) ?><?php if ( $i < $tot ) : ?> ...<?php else : ?></p><?php endif; ?>
<?php if ( $i < $tot ) : 
if ( $container == 'p' || $container == 'div' ) : ?></p><?php endif; 
if ( $container != 'plain' ) : ?><<?php echo $container; ?> class="more"><?php if ( $container == 'div' ) : ?><p><?php endif; endif; ?>
 
<a href="<?php the_permalink(); ?>" title="<?php echo $link_text; ?>"><?php echo $link_text; ?></a><?php
 
if ( $container == 'div' ) : ?></p><?php endif; if ( $container != 'plain' ) : ?></<?php echo $container; ?>><?php endif;
 
if ( $container == 'plain' || $container == 'span' ) : ?></p><?php endif; 
endif;
 
}
?>

The improved excerpt can now be called anywhere in your template files, index.php, home.php, etc, by using:

<?php the_news_excerpt('80','','<strong><em><p>','plain','no'); ?>

This will give you an excerpt of 80 words that preserves the strong, em, and p tags, displays a “more” link without any sort of container element, and does not convert text to smilies.

For a full list of options that can be used with this, check out the plugin author’s page.

About Me

Pippin Williamson is a freelance web designer / developer that has a sometimes unhealthy addiction to coffee and Wordpress.

He builds custom wordpress themes that are specialized for Content Management.

Read more about Pippin.

8 Responses to “Better WordPress Post Excerpt – Revamped”

  1. April 22, 2010 at 8:46 pm

    Great article Pip. I also love wordpress :)

  2. June 12, 2010 at 3:38 am

    I will try this out. I am a newbie wordpress user so I just ignore the function of the excerpt.

    Thanks for sharing the information

    • June 12, 2010 at 3:51 am

      Just ask if you need help with anything, I am more than happy to help out.

  3. Jake
    August 19, 2010 at 2:10 am

    Wow. this is a great tutorial and code. I have been using plugins to do this. But I like your better.

  4. August 31, 2010 at 9:53 am

    I made a custom theme for myself and was using the excerpt reloaded plugin till now ,but now m gonna remove that plugin and use your codes ,thnx a lot for your help.

    • August 31, 2010 at 4:11 pm

      Great, glad to know it’s working well for you.

  5. Jake
    September 1, 2010 at 11:15 am

    Hello. Never used excerpt reloaded plugin before, but I think I will try your suggestion to add the code in the functions.php.

    Thanks for the share.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">