Created
February 19, 2012 14:44
-
-
Save thanosp/1864117 to your computer and use it in GitHub Desktop.
Wordpress custom smart excerpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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