Better WordPress Post Excerpt – Revamped
April 22nd, 2010My 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 »', $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.
Tags: excerpt, tips, tutorial, Wordpress
Posted in Tutorials, Wordpress | 8 Comments »






Great article Pip. I also love wordpress
Thanks Kel, glad you liked it.
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
Just ask if you need help with anything, I am more than happy to help out.
Wow. this is a great tutorial and code. I have been using plugins to do this. But I like your better.
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.
Great, glad to know it’s working well for you.
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.