Skip to content

Instantly share code, notes, and snippets.

@wpexplorer
Created November 26, 2013 01:35
Show Gist options
  • Select an option

  • Save wpexplorer/7652080 to your computer and use it in GitHub Desktop.

Select an option

Save wpexplorer/7652080 to your computer and use it in GitHub Desktop.
Better custom WordPress excerpts
if ( !function_exists( 'wpex_excerpt' ) ) {
function wpex_excerpt($length=30, $readmore=false ) {
global $post;
$id = $post->ID;
$meta_excerpt = get_post_meta( $id, 'wpex_excerpt_length', true );
$length = $meta_excerpt ? $meta_excerpt : $length;
if ( has_excerpt( $id ) ) {
$output = apply_filters( 'the_content', $post->post_excerpt );
} else {
$excerpt = get_the_content('');
$excerpt = do_shortcode( $excerpt );
$excerpt = apply_filters('the_content', $excerpt);
$excerpt = str_replace(']]>', ']]>', $excerpt);
$excerpt = apply_filters('the_content', $excerpt);
$output = '<p>'. wp_trim_words( $excerpt, $length ) .'</p>';
if ( $readmore == true ) {
$readmore_link = '<a href="'. get_permalink( $id ) .'" title="'. __('continue reading', 'wpex' ) .'" rel="bookmark" class="readmore">'. __('continue reading', 'wpex' ) .'<span class="icon-chevron-right"></span></a>';
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
echo $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment