Sometimes you don’t have a featured image with your post. In such a case you would like to have the first image of the post as thumbnail . Here is a snippet to get the first image from post content. sometimes this trick might not work, this occurs when the DOM element has a different structure. So if this doesn’t work for some reason, try adding one more regex which suits your case to make it work. You can also parse the DOM to get the image source. I will cover that topic in the next post. For now, here is how you do it through regex.