Can't get or display post thumbnail in wordpress

i'm working on a WordPress plugin and i'm trying to display/get the post thumbnail below the post on my "Related posts" part. I tried many things which are supposed to work but not for me ... here is the code (on my plugin.php file):

add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );

if ( has_post_thumbnail() ) {
    echo ('there is a thumbnail');
    echo get_the_post_thumbnail(get_the_ID(), "thumbnail"); // nothing
    echo get_the_post_thumbnail(); // nothing
    echo var_dump(get_the_post_thumbnail(get_the_ID(), "thumbnail")); //string '' (length=0)
    echo var_dump(get_the_post_thumbnail()); //string '' (length=0)
    the_post_thumbnail('thumbnail'); // nothing
    the_post_thumbnail(); // nothing
}
else {
    echo ('there is no thumbnail');
}

first i thought that there were no thumbnail for my post but my first echo returned "there is a thumbnail" and all the rest doesn't return anything ... so i don't understand... what is missing ?

Thanks

Edit:

here is the current full code in my plugin.php file:

function Similar_Article ($content) {
    if (is_single() || is_page()) {
        the_post();
        $similar_to_add = '';
        $id = get_the_ID();
        $content_and_similar = $content . 'brbr'; //spaces between post and related posts

        /* many methods called to find my related posts */

        if (has_post_thumbnail()){
            $content_and_similar .= get_the_post_thumbnail(); // it works
        }
        else {
            $imageData = wp_get_attachment_image_src(get_post_thumbnail_id ( $id ), 'thumbnail'); 
            $content_and_similar .= 'img src=" '.$imageData[0].' "';
        }

        $content_and_similar .= $similar_to_add;
        return $content_and_similar;
    }
    else {
        return $content;
    }
}

add_filter( 'the_content', 'Similar_Article' );

my function works if there is a thumbnail but display nothing if there is no thumbnail...

Topic post-thumbnails plugins Wordpress

Category Web


Please notice, has_post_thumbnail() does not only check to see if the post has a featured image, but also checks if there is an image in the post itself. There is a quote from WordPress Code Reference:

It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE.

I suggest you use if (get_the_post_thumbnail() != '') instead of if (has_post_thumbnail()).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.