Skip to content

Instantly share code, notes, and snippets.

@thanosp
Created February 19, 2012 14:44
Show Gist options
  • Save thanosp/1864117 to your computer and use it in GitHub Desktop.
Save thanosp/1864117 to your computer and use it in GitHub Desktop.
Wordpress custom smart excerpt
<?php
/**
* Limits an excerpt to the given number of characters
* @param integer $limit in words
* @param boolean $allowUnfilteredManualExcerpt Gives the manual excerpt priority
* @return string
*/
function getTheLimitedExcerpt($limit = 30, $allowUnfilteredManualExcerpt = false)
{
global $post;
//first select the excerpt
$manualExcerpt = $post->post_excerpt;
if (strlen($manualExcerpt) > 0) {
if ($allowUnfilteredManualExcerpt) {
return $manualExcerpt;
}
$excerpt = $manualExcerpt;
} else {
$excerpt = get_the_excerpt();
}
$words = explode(' ', $excerpt);
//then limit the excerpt
if ($limit > 1 && count($words) > $limit) {
$excerpt = implode(' ', array_slice($words, 0, $limit - 1)) . '...';
}
return $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment