Skip to content

Instantly share code, notes, and snippets.

@jeremyfelt
Created April 10, 2012 00:33
Show Gist options
  • Save jeremyfelt/2347611 to your computer and use it in GitHub Desktop.
Save jeremyfelt/2347611 to your computer and use it in GitHub Desktop.
Display excerpt with some tags
<?php
/* Used to display the excerpt of a post across the site. Written very closely with
* wp_trim_excerpt, but instead of stripping out all tags, we want to make sure that
* some basic style still remains as this display function will be used for the index
* and archive templates, not something as plain as a feed or meta description. */
function prefix_display_excerpt( $number_of_words = 55 ) {
global $post;
$content = get_the_content();
$content = strip_shortcodes( $content );
$content = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
$content = strip_tags( $content, '<p><a><em><strong><del>' );
/* Continue to provide a way for child themes to modify the excerpt
* length and use of ellipsis */
$excerpt_length = apply_filters( 'excerpt_length', $number_of_words );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );
$words_array = preg_split( "/[\n\r\t ]+/", $content, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
if ( count( $words_array ) > $excerpt_length ) {
array_pop( $words_array );
$content = implode( ' ', $words_array );
$content = $content . $excerpt_more;
} else {
$content = implode( ' ', $words_array );
}
echo force_balance_tags( $content ) . ' <a href="'. get_permalink( get_the_ID() ) . '">read more</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment