I love WordPress! It does nearly everything we need it to do, all backed by a fantastic community and a very motivated development team. This is the main reason we have entrusted it to power most of our client’s websites.
Geek Alert!
If you’re not a total developer geek like myself, you’re more than likely not going to understand what I’m talking about from here on out.
I say WordPress does nearly everything because sometimes—like today—I need it to to do something that it doesn’t. It was something so simple, so blatantly missing from the default functions, that I decided to write up a little function to do it for me.
The Problem
WordPress allows you to grab the content of a Page or Post by using the get_the_content function (there are others, but this is what I’m using for this example). Now, it’s great and all, but it just displays the entire contents of the Page or Post. But what if I just want to pull a specific number of words from that post (for News or Blog posts in a list, for example)? Sure, we had the_content_rss but that function is now deprecated and the suggested replacement wasn’t quite as simple or flexible as I needed it to be.
The Solution
I came up with a quick solution that you simply pass a number of characters you’d like to grab to the function, and it will spit out a truncated version of the content, cutting it to the closest word at that word count. Here’s the code:
function am_get_content($length) {
// Set up our variable.
$final_content = '';
// Get's the content the normal way.
$content = strip_tags(get_the_content());
// If that content is longer than your character count, let's shrink it!
if (strlen($content) > $length) {
// Create an array of words from the content.
$content_array = explode(' ',$content);
// Loop through the words, get the character count,
// and then tell it at which word we should stop.
foreach($content_array as $key => $word){
// Are we at the length? Set the key and break out of the loop.
if ($total_length >= $length){
$word_key = $key;
break;
// Otherwise, keep looping through.
} else {
$chars = strlen($word);
$total_length = $total_length + $chars;
}
}
// Now loop through that same array again, this time building
// the $final_content variable based on at which key we should stop.
foreach($content_array as $key => $word){
if ($key <= $word_key){
// Is this the last word in the content? Let's remove any
// punctuation from it so it looks nice!
if ($key == $word_key){
$word = preg_replace('/[^a-zA-Z0-9-\s]/', '', $word);
}
$final_content .= $word.' ';
}
}
// Throw in a "..." so people know there's more to read.
$final_content = $final_content . "...";
}
// Return the $final_content variable!
return $final_content;
}
How to Implement
You’ll notice it also removes punctuation from the last word (if there is any). To call this function, just use the following code within a WordPress Loop (where “50″ in this case would be the character cut length):
am_get_content(50)
The Result
And that’s it! An elegant truncation solution in under 50 lines of code, and here’s what the final result would look like (you’d probably want to add a “Read More” link after it, or simply add options for that to this function):
Feel free to post any questions or comments about my solution below.
Add a Comment