End excerpt at the end of the sentence

Use this function instead. Then put the_excerpt(); in your template.

/**
 * Find the last period in the excerpt and remove everything after it.
 * If no period is found, just return the entire excerpt.
 *
 * @param string $excerpt The post excerpt.
 */
function end_with_sentence( $excerpt ) {

  if ( ( $pos = mb_strrpos( $excerpt, '.' ) ) !== false ) {
    $excerpt = substr( $excerpt, 0, $pos + 1 );
  }

  return $excerpt;
}
add_filter( 'the_excerpt', 'end_with_sentence' );

Leave a Comment