Can’t get or display post thumbnail in wordpress

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()).