Skip to content

Instantly share code, notes, and snippets.

@kayue
Created January 10, 2011 04:18
Show Gist options
  • Save kayue/772349 to your computer and use it in GitHub Desktop.
Save kayue/772349 to your computer and use it in GitHub Desktop.
Get trimmed excerpt (by word) in WordPress
<?php
function get_trimmed_excerpt($maxChars = 160, $appendingString = '...', $default_excerpt = "Default page description.")
{
if( !is_single() && !is_page() )
return $default_excerpt;
the_post();
$content = substr(get_the_excerpt(), 0, $maxChars);
$content = strip_tags($content);
$pos = strrpos($content, " ");
if ($pos > 0) {
$content = substr($content, 0, $pos);
}
$result = $content.$appendingString;
rewind_posts();
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment