Popular Categories

How to get custom excerpt like WordPress?

An excerpt is a short phrase or summary of the whole article. This has been part of our requirements that some times you need a custom excerpt rather than what WordPress offers with the limited text length. Here is the snippet of code which can help you achieve this:

function getExcerpt($str, $startPos=0, $maxLength=100) {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength-3);
$lastSpace = strrpos($excerpt, ‘ ‘);
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= ‘…’;
} else {
$excerpt = $str;
return $excerpt;

If this snippet of code helped you, give us a thumbs up by following us at: Twitter and Facebook.