michaelwilhelmsen
8/14/2015 - 1:30 PM

Custom excerpt that strips all content but text. Customizable word length, ending phrase and "read more" link.

Custom excerpt that strips all content but text. Customizable word length, ending phrase and "read more" link.

function custom_excerpt( $num_words = 45, $ending = '...', $post_id = null, $more_link = "Read more") {
    global $post;

    // Truncate post content
    $current_post = $post_id ? get_post( $post_id ) : $post;
		$excerpt_content = $current_post->post_excerpt;

		if ($excerpt == '') {
			// If excerpt is empty use content,
			$excerpt = strip_shortcodes( $current_post->post_content );
		} else {
			// If excerpt has content use excerpt
			$excerpt = strip_shortcodes( $current_post->post_excerpt );
		}
    $excerpt = wp_trim_words( $excerpt, $num_words, $ending );
    $excerpt = trim($excerpt);


    // Read more link
    $excerpt .= '<a class="readmore-custom" href="' . get_permalink( $post ) . '" title="' . get_the_title( $post ) . '">' . $more_link . '</a>';

    return $excerpt;
}