How to shorthen your wordpress posts into summaries

April 1st, 2010 by Joseph Mas Leave a reply »

Here is a short fix to summarize your blog posts into the first five sentences on your WordPress blog pages. It is quite simple. I am sure there is other ways to do this but I just needed a quick fix. This fix will only take a couple minutes.

I just needed a quick fix for a site I was working on. The posts were long so before the fix, if you have 5-10 posts you have to scroll way down the page. We just needed a summary of like the first 5 sentences of each posting.

You can see it in action here:
http://www.procollectionjobs.com/arm-industry-news/category/credit-collections-finance-and-risk/

After searching for a plugin I did find a couple of them but they did not work with the latest release or my theme. Therefore, I whipped this little fix up.

Are you ready? Heres how I did it:

First, open "/wp-includes/post-template.php" and add the following function to the file:

//—————————————————————————-

function the_content_shortened($more_link_text = null, $stripteaser = 0) {

 $num_of_sentences_post_to_display = 5;
 $content = get_the_content($more_link_text, $stripteaser);
 $content = apply_filters('the_content', $content);
 $content = str_replace(']]>', ']]>', $content);

 $contentARR = explode(".",$content);

 $shortened_content = "";
 for($i=0;$i<$num_of_sentences_post_to_display;$i++)
 {
   $shortened_content .= $contentARR[$i].". ";
 }
 $shortened_content = force_balance_tags($shortened_content . " …");
 echo $shortened_content;
 echo '(<a href="' . get_permalink() . "\"#more-$id\" class=\"more-link\">Read full news article</a>)";
}

//—————————————————————

Next, find and open the following file:
What ever your theme is replace "yourtheme" below with your theme.

/wp-content/themes/yourtheme/index.php

Now, in the file replace this line (yours might be slightly different but "the_content" function is what we are replacing, just look for it):

<?php the_content('<span>Read the rest of this entry…</span>'); ?>
with this:
<?php the_content_shortened('<span>Read the rest of this entry…</span>'); ?>

Save your work and WAAAAA…… BAMMM! thats it. All done.

Note: you may want to apply the fix to these other pages also as they usualy show multiple posts like your home page.

Here are the most common files that you would be interested in:
/wp-content/themes/yourtheme/index.php
/wp-content/themes/yourtheme/archive.php
/wp-content/themes/yourtheme/archives.php
/wp-content/themes/yourtheme/search.php
/wp-content/themes/yourtheme/page.php

Let me know if this helps you.
Joe

Advertisement

Leave a Reply

Bookmark & Share