Extract the_excerpt from the_content

I’m trying to create a filter to display only the text of a the_content without the images. But not working, I tried it but it does not display anything, it is blank

add_filter('the_content', 'importgaleria');

function importgaleria($content) {
    global $post;
    if (is_singular()) {
        $content=$post->post_excerpt;
        return $content;
    } else {
        return $content;
    }
}

Related posts

Leave a Reply

2 comments

  1. I managed to solve with this code

    add_filter('the_content', 'importgaleria');
    
    function importgaleria($content) {
           if (is_singular()) {
            $content =  wp_trim_words(get_the_content(), 20, '' ) ;
            return $content;
    
        } else {
            return $content;
         };
    
    };
    
  2. You can try below code

    add_filter('the_content', 'importgaleria');
    
    function importgaleria($content) {
        global $post;
        if (is_singular()) {
            $content=$post->post_excerpt;
            $content = preg_replace('/(<)([img])(w+)([^>]*>)/', '', $content);
            $content = str_replace(']]>', ']]&gt;', $content);
            return $content;
        } else {
            return $content;
         };
    
    };