1

I need to create an excerpt that doesn't stop with an orphan word such as:

All I’ve got to do is pass as an ordinary human being. Simple. What could possibly go wrong? Did I mention we have comfy chairs? I’m the Doctor, I’m worse than everyone’s aunt. catches himself And that is not how I’m introducing myself.You hit me with a cricket bat. It’s more...Read More

I need it to end with "You hit me with a cricket bat" (the last complete sentence is where I want it to stop).

I found this on another post:

add_filter('get_the_excerpt', 'end_with_sentence');

function end_with_sentence($excerpt) {
  $allowed_end = array('.', '!', '?', '...');
  $exc = explode( ' ', $excerpt );
  $found = false;
  $last = '';
  while ( ! $found && ! empty($exc) ) { 
    $last = array_pop($exc);
    $end = strrev( $last );
    $found = in_array( $end{0}, $allowed_end );
  }
  return (! empty($exc)) ? $excerpt : rtrim(implode(' ', $exc) . ' ' .$last);
}

and then I add this to my template:

<?php get_the_excerpt(); ?>

But it doesn't seem to work. It doesn't display anything.

What am I doing wrong?

  • echo get_the_excerpt(); – ngearing Oct 6 '16 at 23:59
  • @Nath that worked. I forgot that I had to echo it. thanks. Post this as the answer and I will accept . – agon024 Oct 7 '16 at 0:57
  • @agon024 did you try my function below? It's much cleaner. – cowgill Oct 7 '16 at 1:11
1

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' );
| improve this answer | |
  • that only removes the "...Read More >>" It still leaves an sentence orphan. – agon024 Oct 7 '16 at 1:50
  • You must be using a custom excerpt_more filter to modify the standard "Read More" or [...] output to "...Read More". If you remove the three dots, it'll work. – cowgill Oct 7 '16 at 2:04
1

This will take anything you throw at it ;-) Plus, it's easy to read (kidding, I'm just kidding).

P.S. must be PHP 5.4 or greater...

function end_with_sentence( $excerpt ) {
    // change the '...' to whatever your "read more" string is; default in WP is '...'
    $excerpt = explode( '(#~)', str_replace( ['...','? ','! ','. '], ['($/s$/)','?(#~)','!(#~)','. (#~)'], preg_replace( '!\s+!', ' ', trim( $excerpt ) ) ) );
    return ( !strpos( end( $excerpt ), '($/s$/)' ) ) ? implode( ' ', $excerpt ) : implode( ' ', array_slice( $excerpt, 0, -1 ) );
}
| improve this answer | |
0

You need to echo the function get_the_excerpt().

So in your template use:

echo get_the_excerpt();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.