Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Created September 2, 2012 20:50
Show Gist options
  • Select an option

  • Save pippinsplugins/3604429 to your computer and use it in GitHub Desktop.

Select an option

Save pippinsplugins/3604429 to your computer and use it in GitHub Desktop.
/*
* Gets the excerpt of a specific post ID or object
* @param - $post - object/int - the ID or object of the post to get the excerpt of
* @param - $length - int - the length of the excerpt in words
* @param - $tags - string - the allowed HTML tags. These will not be stripped out
* @param - $extra - string - text to append to the end of the excerpt
*/
function rcp_excerpt_by_id($post, $length = 50, $tags = '<a><em><strong><blockquote><ul><ol><li><p>', $extra = ' . . .') {
if(is_int($post)) {
// get the post object of the passed ID
$post = get_post($post);
} elseif(!is_object($post)) {
return false;
}
$more = false;
if(has_excerpt($post->ID)) {
$the_excerpt = $post->post_excerpt;
} elseif(strstr($post->post_content,'<!--more-->')) {
$more = true;
$length = strpos($post->post_content, '<!--more-->');
$the_excerpt = $post->post_content;
} else {
$the_excerpt = $post->post_content;
}
$tags = apply_filters( 'rcp_excerpt_tags', $tags );
if($more) {
$the_excerpt = strip_shortcodes( strip_tags( stripslashes( substr($the_excerpt, 0, $length) ), $tags) );
} else {
$the_excerpt = strip_shortcodes(strip_tags(stripslashes($the_excerpt), $tags));
$the_excerpt = preg_split('/\b/', $the_excerpt, $length * 2+1);
$excerpt_waste = array_pop($the_excerpt);
$the_excerpt = implode($the_excerpt);
$the_excerpt .= $extra;
}
return wpautop($the_excerpt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment