I grabbed the code from here

I have this in my post right now:

"Test sentence for excerpt purposes.



But this is what's being outputted right now:

"Test sentence for excerpt..."

This is how I'm calling it in my code: $content = get_excerpt(320, 'content'); what I don't understand is if it's set at 320 it should grab all of this content, but it's not.

Here's the excerpt function:

function get_excerpt($limit, $source = null){

    if($source == "content" ? ($excerpt = get_the_content()) : ($excerpt = get_field('bio')));
        $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
        $excerpt = strip_shortcodes($excerpt);
        $excerpt = strip_tags($excerpt);
        $excerpt = substr($excerpt, 0, $limit);
        $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
        $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
        $excerpt = '<p>' . $excerpt . '...</p><div class="read-more"><a href="' . get_permalink($post->ID) . '" class="bysc-btn-text bysc-btn-text-primary">Read More</a></div>';
    return $excerpt;

Why is the text being cut off early? Shouldn't it show it all?

  • It's not clear from your post -- did you hook up your get_excerpt() function with add_filter( 'excerpt_length', 'get_excerpt', 999 );? Also, you should add a personalized prefix to the function name get_excerpt, i.e. darren_get_excerpt to avoid naming collisions. – Dave Romsey Mar 13 '18 at 18:23
  • 1
    @DaveRomsey No I'm not using an add_filter. I'm using the $content I mentioned in my post inside my loop. – Darren Mar 13 '18 at 18:28
  • 1
    strripos($excerpt, " ") will always cut off the last word. Your function is ignoring UTF-8 characters, so it will break other things inevitably. :) See this answer for a better version. – fuxia Mar 13 '18 at 19:04
  • @fuxia interesting, I guess in the SO page I linked, the person who wrote the code made an error. – Darren Mar 14 '18 at 1:21

Your Answer

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

Browse other questions tagged or ask your own question.