There are always a group of our readers who are trying to graduate the beginner level early and step into WordPress development. Many of them start by hacking their WordPress themes, creating child themes, or writing plugins. This makes them come across puzzling challenges. One such reader recently asked us how to get the post thumbnail URL in WordPress. The goal here is to just get the post thumbnail URL and then use it with their own markup. In this article, we will show you how to get the post thumbnail URL in WordPress.
Note: You must know the basics of how WordPress themes work.
Simply paste this code inside the loop code that you are writing.
<?php $thumb_id = get_post_thumbnail_id(); $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true); echo $thumb_url; ?>
Make sure to replace
thumbnail-size to whatever image size you want to get. Default sizes are thumbnail, medium, large, and full. You can also use add and use additional image sizes. If you are using a custom image size then make sure to regenerate thumbnails. You can also set a default fallback image for post thumbnails.
What this code does is that we first get the post thumbnail ID. Then we use that id to get the image path or URL using
wp_get_attachment_image_src function, which returns an array that you can use with your own code.
We hope this article helped you learn how to get post thumbnail URL in WordPress. For feedback and questions, please leave a comment below.