• Resolved catmaniax

    (@catmaniax)


    Hello everyone!

    As the title says, I’d like to know if there’s a way (some code to put in functions.php maybe) to remove or just hide a specific word from posts’ excerpts.

    Please let me know.
    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You could use https://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/get_the_excerpt to get the excerpt and do a strreplace to remove the unwanted words.

    Thread Starter catmaniax

    (@catmaniax)

    Thanks for the reply.

    I don’t write anything in the excerpt section of the backend, it just grabs the content to display as the excerpt.

    Can you please explain how I can easily remove the unwanted words, because I did not understand.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Something like this… NOTE: THIS IS NOT TESTED ad may or may not work. YOU MAY HAVE TO DEBUG.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt;
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
    Thread Starter catmaniax

    (@catmaniax)

    That did the trick quite nicely!

    Thank you.

    One more question.
    Any idea how to add … to the last word of the excerpt?

    • This reply was modified 8 years, 7 months ago by catmaniax.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    $excerpt .= " just one more thing... "
    return $excerpt;
    Thread Starter catmaniax

    (@catmaniax)

    I get a parse error when I paste above code along with the first code you posted 🙁

    btw, thanks again!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    If you can’t debug the code, show me the code you are using via pastebin.com or gist.github.com

    This is why I don’t like giving people code!

    Thread Starter catmaniax

    (@catmaniax)

    I didn’t use any different code, I just don’t know how to make the two codes you gave me both work.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt;
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
    
    $excerpt .= " just one more thing... "
    return $excerpt;
    
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    No, no, no.

    Put $excerpt .= " just one more thing... " above return $excerpt INSIDE the function and delete those last two lines.

    Thread Starter catmaniax

    (@catmaniax)

    But wouldn’t doing that replace the function of not showing the bad words?

    Sorry for asking again, but could you please post the whole correct code including both of those functions?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    No. Look up what the “dot equal” operator does in PHP.

    function my_clean_excerpt( $excerpt ) {
       $badwords = array( 
          'word1',
          'word2',
          'word3',
        );
       foreach ( $badwords as $badword ) {
           $excerpt = str_replace( $badword, '', $excerpt);
           }
       return $excerpt . ' one more thing... ';
    }
    add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
    Thread Starter catmaniax

    (@catmaniax)

    Thank you.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Remove/hide specific word from excerpt?’ is closed to new replies.