WordPress Truncation call is causing php4 errors

My latest WordPress theme, DynamicColor has been doing rather well as far as the downloads go. I was suprised to here that several people were having some trouble with this theme. It seems that they are still running php4. I have been working rather hard to master Classes within php. Therefore, I have been using classes when ever possible. Turns out these classes are causing their errors.

I built a truncating class that I like more then the WordPress default get_excerpt(). Ive never been happy having to add more tags to a post. Therefor in all my themes I ad some form of truncation. My latest solution has been the class found below.

class truncate{
/* Public function for truncating content
*  Requires an string and a length
*
* Structure:
* $truncateString = ‘text ro truncate’
*
* Example:
*    $truncateString = truncate::doTruncate($_POST[‘truncate’], 100);
*
* Returns:
* return truncated string;
*/

public static function doTruncate($truncateString, $limit, $break=”.”, $pad=”…”) {

// return with no change if string is shorter than $limit
if(strlen($truncateString) <= $limit) return $truncateString;

// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($truncateString, $break, $limit))) {

if($breakpoint < strlen($truncateString) – 1) {

$truncateString = substr($truncateString, 0, $breakpoint) . $pad;

}

}
return $truncateString;

}

}

With this class I am able to use:

<?php $permalink = ‘ <a href=”‘.get_permalink().'”>Read More…</a>’;  ?>
<?php $truncateContent = truncate::doTruncate(strip_tags(get_the_content(), ”), 300, ‘.’, $permalink); ?>
<p><?php echo $truncateContent; ?></p>

Instead of using the typical the_excerpt() or the the_content. I can specify the amount of truncation as well as what I want character I want to stop the truncation at. The example above truncates on a period.

If you are building a WordPress theme and are looking for a better way to truncate the content, feel free to use this class. Clearly, I would love a link back to this article if you do end up using it. But consider yourself warned, there are a few people still using php4 and they will get an error.

If you would like to see the them you can check it out at: WordPress.GordonFrench.com

Published by

Gordon French

Gordon French was born in Wyoming in 1978. He is married to Tiffany French. He has two children Halie and Zac. His first passion of course family with cars coming in a very close second.

Leave a Reply

Your email address will not be published. Required fields are marked *