A smarter way to get the post thumbnail

Post thumnails are a new feature introduced in WordPress 2.9 and it lets you assign a representative image to your posts/pages/custom posts. It is very useful in archive pages where you have limited space for each article.
But what if you don’t have a featured image for your post? What if you don’t need or want an image with no other use than to represent your post? Displaying an image in the archive pages is still pretty useful so to simplify things I added this funciton to my Theme Functions file:

In other words, it will display the post thumbnail as usual, if the post has one. If it doesn’t, the code will return the thumbnail of one of the images attached to that post. If you inserted images in your post, they are automatically attached. And there you go, don’t have time to find a featured image for a post? Don’t worry about it!

Related posts

Comments are closed.